【发布时间】:2015-07-03 06:10:20
【问题描述】:
我需要将此 JSON 字符串解析为“WeatherJson”类型的对象。但是我不知道如何解析字符串中的数组,例如 '"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}]。实体类会是什么样子?
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
【问题讨论】:
-
那么您的
WeatherJson类型是什么样的?您是否已经有一些代码可以尝试解析?如果是这样,当您尝试时会发生什么? -
看看这个[如何在C#中解析json][1][1]:stackoverflow.com/a/6620173/1129313
-
我确实知道如何解析普通的 json 数据,例如:message":0.0145。我的问题是检索数组并将其存储在 @JonSkeet 对象中
-
又一次,你有什么尝试?只需在您的数据类型中将它们设为数组或列表即可。 (顺便说一句,我已重新格式化 JSON 以使其更易于阅读。)
-
好吧,still 还没有显示
WeatherJson类型,因为weather是一个数组,它应该是你类型中的一个数组 - 所以你会需要w.Weather[0].Description。