使用 JSON.net 反序列化 JSON 数组
Deserializing JSON Array with JSON.net
我有一些来自 EPA 的紫外线指数 API 的 JSON。 json 是一个包含单独元素的数组。
JSON示例:
[
{
"ORDER": 1,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 07 AM",
"UV_VALUE": 0
},
{
"ORDER": 2,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 08 AM",
"UV_VALUE": 1
},
{
"ORDER": 3,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 09 AM",
"UV_VALUE": 1
},
{
"ORDER": 4,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 10 AM",
"UV_VALUE": 2
},
{
"ORDER": 5,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 11 AM",
"UV_VALUE": 3
},
{
"ORDER": 6,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 12 PM",
"UV_VALUE": 7
},
{
"ORDER": 7,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 01 PM",
"UV_VALUE": 6
},
{
"ORDER": 8,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 02 PM",
"UV_VALUE": 6
},
{
"ORDER": 9,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 03 PM",
"UV_VALUE": 7
},
{
"ORDER": 10,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 04 PM",
"UV_VALUE": 5
},
{
"ORDER": 11,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 05 PM",
"UV_VALUE": 3
},
{
"ORDER": 12,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 06 PM",
"UV_VALUE": 1
},
{
"ORDER": 13,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 07 PM",
"UV_VALUE": 0
},
{
"ORDER": 14,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 08 PM",
"UV_VALUE": 0
},
{
"ORDER": 15,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 09 PM",
"UV_VALUE": 0
},
{
"ORDER": 16,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 10 PM",
"UV_VALUE": 0
},
{
"ORDER": 17,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 11 PM",
"UV_VALUE": 0
},
{
"ORDER": 18,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 12 AM",
"UV_VALUE": 0
},
{
"ORDER": 19,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 01 AM",
"UV_VALUE": 0
},
{
"ORDER": 20,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 02 AM",
"UV_VALUE": 0
},
{
"ORDER": 21,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 03 AM",
"UV_VALUE": 0
}
]
为了解析它,我假设我可以只使用 foreach
语句来检索每个元素的 UV 索引值 属性。 b.Result就是上面的JSON.
JObject EPAData = JObject.Parse(b.Result);
foreach (var UVIndex in EPAData.Root)
{
string uv = (string)UVIndex["UV_VALUE"];
//Do whatever I want with the UV index
};
但是,这似乎不起作用。我收到一条错误消息:
Error in application: Error reading JObject from JsonReader. Current
JsonReader item is not an object: StartArray. Path '', line 1,
position 1.
你的 JSON 代表一个数组,而不是一个对象,所以用 JArray.Parse
代替:
JArray EPAData = JArray.Parse(b.Result);
foreach (var UVIndex in EPAData)
{
Console.WriteLine (UVIndex["UV_VALUE"]); // 0, 1, 1, etc.
}
如果你想得到Uv_VALUE的值只是很简单,你可以使用这个代码:
var json = JArray.Parse(s);
var list_items = json.Children<JObject>().Properties().Where(p => p.Name == "UV_VALUE").ToList();
list_items.ForEach(c=>Console.WriteLine(c.Name+" "+c.Value));
我有一些来自 EPA 的紫外线指数 API 的 JSON。 json 是一个包含单独元素的数组。 JSON示例:
[
{
"ORDER": 1,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 07 AM",
"UV_VALUE": 0
},
{
"ORDER": 2,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 08 AM",
"UV_VALUE": 1
},
{
"ORDER": 3,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 09 AM",
"UV_VALUE": 1
},
{
"ORDER": 4,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 10 AM",
"UV_VALUE": 2
},
{
"ORDER": 5,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 11 AM",
"UV_VALUE": 3
},
{
"ORDER": 6,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 12 PM",
"UV_VALUE": 7
},
{
"ORDER": 7,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 01 PM",
"UV_VALUE": 6
},
{
"ORDER": 8,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 02 PM",
"UV_VALUE": 6
},
{
"ORDER": 9,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 03 PM",
"UV_VALUE": 7
},
{
"ORDER": 10,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 04 PM",
"UV_VALUE": 5
},
{
"ORDER": 11,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 05 PM",
"UV_VALUE": 3
},
{
"ORDER": 12,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 06 PM",
"UV_VALUE": 1
},
{
"ORDER": 13,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 07 PM",
"UV_VALUE": 0
},
{
"ORDER": 14,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 08 PM",
"UV_VALUE": 0
},
{
"ORDER": 15,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 09 PM",
"UV_VALUE": 0
},
{
"ORDER": 16,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 10 PM",
"UV_VALUE": 0
},
{
"ORDER": 17,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 11 PM",
"UV_VALUE": 0
},
{
"ORDER": 18,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 12 AM",
"UV_VALUE": 0
},
{
"ORDER": 19,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 01 AM",
"UV_VALUE": 0
},
{
"ORDER": 20,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 02 AM",
"UV_VALUE": 0
},
{
"ORDER": 21,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 03 AM",
"UV_VALUE": 0
}
]
为了解析它,我假设我可以只使用 foreach
语句来检索每个元素的 UV 索引值 属性。 b.Result就是上面的JSON.
JObject EPAData = JObject.Parse(b.Result);
foreach (var UVIndex in EPAData.Root)
{
string uv = (string)UVIndex["UV_VALUE"];
//Do whatever I want with the UV index
};
但是,这似乎不起作用。我收到一条错误消息:
Error in application: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
你的 JSON 代表一个数组,而不是一个对象,所以用 JArray.Parse
代替:
JArray EPAData = JArray.Parse(b.Result);
foreach (var UVIndex in EPAData)
{
Console.WriteLine (UVIndex["UV_VALUE"]); // 0, 1, 1, etc.
}
如果你想得到Uv_VALUE的值只是很简单,你可以使用这个代码:
var json = JArray.Parse(s);
var list_items = json.Children<JObject>().Properties().Where(p => p.Name == "UV_VALUE").ToList();
list_items.ForEach(c=>Console.WriteLine(c.Name+" "+c.Value));