2017-06-06 139 views
-1

我試圖接收從作爲JSON這樣請求響應數據:如何從JSON獲取數據並將其轉換爲對象?

{ 「CNT」:1, 「列表」:[{ 「座標」:{ 「LON」: 18.55, 「LAT」:50.11 }, 「SYS」:{ 「類型」:1, 「ID」:5356, 「消息」:0.0095, 「國家」: 「PL」, 「日出「:1496630290, 」sunset「:1496688673 }, 」weather「:[{ 「ID」:800, 「主」: 「清除」, 「描述」: 「晴天」, 「圖標」: 「01D」 }], 「主」:{ 「TEMP」:293.71 , 「壓力」:1014, 「溼度」:42, 「temp_min」:293.15, 「temp_max」:294.15 }, 「能見度」:10000, 「風」:{ 「速度」:4.1 , 「DEG」:60 }, 「雲」:{ 「所有」:0 }, 「DT」:1496686603, 「ID」:7531758, 「名」: 「雷布尼克」 }]}

我試圖用JSON.NET反序列化JSON和接收數據。但我不知道如何正確地做到這一點。 我試圖用我的類方法來實現這一目標:

public Rootobject DeserializeJSON() 
    { 
     private JObject responseObject; 

     private JToken responseToken; 

     private JArray responseArray; 


     responseObject = JObject.Parse(json); 

     Rootobject Weather = new Rootobject(); 

     responseArray = (JArray)responseObject.SelectToken("list"); 

     responseToken = (JToken)responseArray.SelectToken("main"); 

     Weather = responseToken.ToObject<Rootobject>(); 

     return Weather; 
    } 

,但它似乎並不工作。在這種情況下,我想收到來自「主」的屬性數據,JSON,並轉換到我的類對象:

class Main 
{ 
    public float temp { get; set; } 

    public float pressure { get; set; } 

    public float humidity { get; set; } 

    public float temp_min { get; set; } 

    public float temp_max { get; set; } 
} 

你能解釋我,應該如何工作以及在那裏是我的錯嗎?

+1

這就像是一個bizillion重複問題關於這一點,更不用說json.net上的一個可愛的幫助頁面。不需要定製方法。 – MickyD

+0

是的,你說得對。在詢問...之​​前,我試圖首先找到解決方案,但我沒有足夠的嘗試。對不起復制。下次我會更耐心。 –

回答

3

首先,你需要聲明以下類:

public class Coord 
{ 
    [JsonProperty("lon")] 
    public double Lon { get; set; } 

    [JsonProperty("lat")] 
    public double Lat { get; set; } 
} 

public class Sys 
{ 
    [JsonProperty("type")] 
    public int Type { get; set; } 

    [JsonProperty("id")] 
    public int Id { get; set; } 

    [JsonProperty("message")] 
    public double Message { get; set; } 

    [JsonProperty("country")] 
    public string Country { get; set; } 

    [JsonProperty("sunrise")] 
    public int Sunrise { get; set; } 

    [JsonProperty("sunset")] 
    public int Sunset { get; set; } 
} 

public class Weather 
{ 
    [JsonProperty("id")] 
    public int Id { get; set; } 

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

    [JsonProperty("description")] 
    public string Description { get; set; } 

    [JsonProperty("icon")] 
    public string Icon { get; set; } 
} 

public class Main 
{ 
    [JsonProperty("temp")] 
    public double Temp { get; set; } 

    [JsonProperty("pressure")] 
    public int Pressure { get; set; } 

    [JsonProperty("humidity")] 
    public int Humidity { get; set; } 

    [JsonProperty("temp_min")] 
    public double TempMin { get; set; } 

    [JsonProperty("temp_max")] 
    public double TempMax { get; set; } 
} 

public class Wind 
{ 

    [JsonProperty("speed")] 
    public double Speed { get; set; } 

    [JsonProperty("deg")] 
    public int Deg { get; set; } 
} 

public class Clouds 
{ 

    [JsonProperty("all")] 
    public int All { get; set; } 
} 

public class List 
{ 
    [JsonProperty("coord")] 
    public Coord Coord { get; set; } 

    [JsonProperty("sys")] 
    public Sys Sys { get; set; } 

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

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

    [JsonProperty("visibility")] 
    public int Visibility { get; set; } 

    [JsonProperty("wind")] 
    public Wind Wind { get; set; } 

    [JsonProperty("clouds")] 
    public Clouds Clouds { get; set; } 

    [JsonProperty("dt")] 
    public int Dt { get; set; } 

    [JsonProperty("id")] 
    public int Id { get; set; } 

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

public class Example 
{ 

    [JsonProperty("cnt")] 
    public int Cnt { get; set; } 

    [JsonProperty("list")] 
    public IList<List> List { get; set; } 
} 

然後你可以按照以下反序列化JSON:

var example = JsonConvert.DeserializeObject<Example>(jsonString) 

其中jsonString是你的JSON。

PS。我使用jsonutils和你的json字符串推導出上述類。

+0

@Downvoter我會誠實地對你的downvote進行推理。在此先感謝 – Christos

+0

我正在考慮這一點。但我不需要所有這些數據。我完全需要Main和Name屬性。無論如何宣佈所有這些類是否有意義? –

+0

因此https:// jsonutils。com /會自動pascal-case的屬性,並可選地添加'[JsonProperty]'屬性?謝謝 - 我之前沒有看過那個網站。 – dbc

相關問題