如何将此JSON结果解析为对象
本文关键字:对象 结果 JSON | 更新日期: 2023-09-27 18:00:52
我需要将这个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
您只需使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; }
}
这样尝试:
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");
}
}
}
试试这个,希望它能帮助你。。。。
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.
尝试双重迭代,
for (key in parent) {
for(key1 in parent[key]){
//
}
}
JObject json = JObject.Parse(JsonString);
您可能需要参考Json.NET文档。
如果你有对象的集合,那么你可以使用JArray
JArray jarr = JArray.Parse(JsonString);
有关文档,请参阅此处。
将JArry转换为List只需输入以下方法。它会归还你需要的东西。
Jarray.ToObject<List<SelectableEnumItem>>()
您可以使用JavaScriptSerializer类将JSON解析为Object。
请参阅此StackOverflow线程