2016-04-03 30 views
1

Aşağıdaki json vardır:bir nesne C# Deserialize

{ 
    "coord":{"lon":-88.92,"lat":44.46}, 
    "weather":[{"id":600,"main":"Snow","description":"light snow","icon":"13d"}], 
    "main":{"temp":271.72,"pressure":1009,"humidity":73,"temp_min":269.15,"temp_max":273.15}, 
    "name":"XXXXX", 
} 

ve bu sınıfları kullanarak C# serisini çalışıyorum:

public class weatherClass 
{ 

    [JsonProperty("name")] 
    public string Name { get; set;} 

    [JsonProperty("main")] 
    public Info Main { get; set;} 

    [JsonProperty("weather")] 
    public List<InfoWeather> Weather { get; set; } 
} 

public class Info{ 
    public string temp { get; set;} 
    public string pressure { get; set;} 
} 

public class InfoWeather { 
    public string description { get; set;} 
    public string main { get; set;} 
} 

elimden Bilgisi Sınıf gelen sıcaklığına ve basınca acces . Ama ben InfoWeather ile sorun yaşıyorum. Ben enter image description here

+0

weatherlass.Weather.description

kullanmak Sen sorununuzu gösteren bir [MCVE] vermeniz gerekiyorsa

weatherResult.Text = string.Format("The city is: {0} and the description is: {1}", weatherlass.Name, weatherlass.Weather); 

: Bir şey döndürmez. Ya da bakmanın başka bir yolu da kodunuzu VS veya LINQPad'e kopyalayıp yapıştırabilmem ve onu çalıştırabilmem. – Enigmativity

+0

İşte benim sorunum: 'weatherResult.Text = string.Format (" Şehir: {0} ve açıklama: {1} ", weatherlass.Name, weatherlass.Weather); '[link] kullanıyorum (https://www.dropbox.com/s/3zv8zr2aop2vgs1/Screen%20Shot%202016-04-02%20at%2022.03.16.png?dl=0) 'weatherlass.Weather.description'ı kullanırsam '. Hata verir! – Seph1603

+0

Sorunuzu, fazladan metin ve resim içerecek şekilde düzenleyebilir misiniz - resme harici olarak bağlanma, bunun yerine katıştırma - ve serileştirmek için kullandığınız kodu koyabilir miyim? Sorunuzda bir kopyala ve yapıştır örneği oluşturmalısınız. – Enigmativity

cevap

1
/*Change your class definitions to use proper case names then use the camel case converter provide by newtonsoft*/ 

public class WeatherClass 
{ 
    public string Name { get; set; } 
    public Info Main { get; set; } 
    public List<InfoWeather> Weather { get; set; } 
} 

public class Info 
{ 
    public string Temp { get; set; } 
    public string Pressure { get; set; } 
} 

public class InfoWeather 
{ 
    public string Description { get; set; } 
    public string Main { get; set; } 
} 

var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; 
var weatherClass = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherClass>(json, jsonSerializerSettings); 
+0

Teşekkürler @Vincent ama yine de aynı sorun yaşıyorum: [Image] (https://www.dropbox.com/s/bpc4xz3hoqefivo/Screen%20Shot%202016-04-02%20at%2022.21.20.png?dl= 0) – Seph1603

+1

Ardından burada çalıştırdığım ve mükemmel çalıştığından bir serileştirme sorunu değil. Weather nesneniz bir Liste. WeatherClass.Weather [0] 'ı deneyin. Açıklama – Vincent

+0

Mükemmel !! Teşekkürler. Bu ihtiyacım vardı :) – Seph1603