2015-07-03 112 views
-1

我需要解析這個JSON字符串到我的「WeatherJson」類型的對象。但是我不知道如何解析字符串中的數組,如''weather':[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}]。實體類將是什麼樣子?如何解析這個JSON結果作爲一個對象?

JSON字符串:

{ 
    "coord": {"lon":79.85,"lat":6.93}, 
    "sys": { 
    "type": 1, 
    "id": 7864, 
    "message": 0.0145, 
    "country": "LK", 
    "sunrise": 1435883361, 
    "sunset": 1435928421 
    }, 
    "weather": [ 
    {"id":802, "main":"Clouds", "description":"scattered clouds", "icon":"03d"} 
    ], 
    "base": "stations", 
    "main": { 
    "temp": 302.15, 
    "pressure": 1013, 
    "humidity": 79, 
    "temp_min": 302.15, 
    "temp_max": 302.15 
    }, 
    "visibility":10000, 
    "wind": { "speed": 4.1, "deg": 220 }, 
    "clouds": { "all": 40 }, 
    "dt": 1435893000, 
    "id":1248991, 
    "name":"Colombo", 
    "cod":200 
} 

編輯

我需要檢索從代碼以下值:

WeatherJson w = new WeatherJson(); 
Console.WriteLine(w.weather.description); 
//that above line was retrieved and stored from the JSONArray named 'weather' in the main json response 
+0

那麼你的'WeatherJson'類型是什麼樣子?你有一些代碼已經嘗試解析?如果是這樣,當你嘗試時會發生什麼? –

+0

看看這個[如何分析在C#中的json] [1] [1]:http://stackoverflow.com/a/6620173/1129313 – Garry

+0

我不知道如何解析正常JSON數據如:消息「:0.0145。我的問題是檢索數組並將其存儲在對象@JonSkeet – Dinuka

回答

4

你應該只讓在JSON匹配的陣列,在你的POCO列表或數組類型。下面是使用您所提供的JSON短,但完整的例子:

using System; 
using System.Collections.Generic; 
using System.IO; 
using Newtonsoft.Json; 

class Test 
{ 
    static void Main(string[] args) 
    { 
     var json = File.ReadAllText("weather.json"); 
     var root = JsonConvert.DeserializeObject<Root>(json); 
     Console.WriteLine(root.Weather[0].Description); 
    } 
} 

public class Root 
{ 
    // Just a few of the properties 
    public Coord Coord { get; set; } 
    public List<Weather> Weather { get; set; } 
    public int Visibility { get; set; } 
    public string Name { get; set; } 
} 

public class Weather 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
} 

public class Coord 
{ 
    public double Lon { get; set; } 
    public double Lat { get; set; } 
} 
+0

謝謝!這解釋了我需要知道的一切! – Dinuka

1

嘗試雙迭代,

for (key in parent) { 
    for(key1 in parent[key]){ 
    // 
    } 
} 
2

嘗試這樣的:

void Main() 
{ 
    var json = System.IO.File.ReadAllText(@"d:\test.json"); 

    var objects = JArray.Parse(json); // parse as array 
    foreach(JObject root in objects) 
    { 
     foreach(KeyValuePair<String, JToken> app in root) 
     { 
      var appName = app.Key; 
      var description = (String)app.Value["Description"]; 
      var value = (String)app.Value["Value"]; 

      Console.WriteLine(appName); 
      Console.WriteLine(description); 
      Console.WriteLine(value); 
      Console.WriteLine("\n"); 
     } 
    } 
} 
1

JObject定義方法來解析該:

JObject json = JObject.Parse(JsonString); 

您可能要參考Json.NET documentation

如果你有對象的集合,那麼你可以使用JArray

JArray jarr = JArray.Parse(JsonString); 

對於文檔,你可以看到here

將JArry轉換爲列表只需將下面的方法。它會返回你所需要的。

Jarray.ToObject<List<SelectableEnumItem>>() 
1

試試這個,希望它會幫助你....

 var dataObj=JSON.parse {"coord":{"lon":79.85,"lat":6.93},"sys": 
     {"type":1,"id":7864,"message":0.0145,"country":"LK", 
"sunrise":1435883361, 
"sun  set":1435928421},"weather":  [{"id":802,"main":"Clouds", 
    "description":"scattered clouds","icon":"03d"}], 
    "base":"stations","main":{"temp":302.15,"pressure":1013, 
"humidity":79,"temp_min":302.15, 
"temp_max":302.15},"visibility":10000,"wind":{"speed":4.1,"deg":220}, 
"clouds":{"all":40},"dt":1435893000,"id":1248991, 
    "name":"Colombo","cod":200} 

to read this . 
i m reading one single string so u can able to know. 

var windData=dataObj.wind.speed; 
it will read value 4.1 from your string like this.. 

If u want to read array of string then.. 
var weatherObj=dataObj.weather[0].description; 

so it will read description value from array.like this u can read. 
0

您可以使用JavaScriptSerializer類解析JSON到對象。

請參考這StackOverflow線程