我需要将此JSON字符串解析为类型" WeatherJson"的对象。但是我不知道如何解析字符串中的数组,例如'"":[{"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
答案 0 :(得分:5)
您应该只使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; }
}
答案 1 :(得分: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");
}
}
}
答案 2 :(得分:2)
试试这个希望它会帮助你......
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.
答案 3 :(得分:1)
尝试双重迭代,
for (key in parent) {
for(key1 in parent[key]){
//
}
}
答案 4 :(得分:1)
JObject为此定义方法Parse:
JObject json = JObject.Parse(JsonString);
您可能想要引用Json.NET documentation。
如果你有对象的集合,那么你可以使用JArray
JArray jarr = JArray.Parse(JsonString);
有关文档,您可以看到here。
将JArry转换为List只是放在下面的方法。它会返回你需要的东西。
Jarray.ToObject<List<SelectableEnumItem>>()
答案 5 :(得分:0)
您可以使用JavaScriptSerializer类将JSON解析为Object。
请参阅此StackOverflow主题