如何解析";临时工;来自C#中的Json?
我需要从这个JSON格式中获取“temp”、名称和描述。我可以把其中的一部分拿出来,但不是全部。我把问题都问了一遍,但没有得到具体的答案如何解析";临时工;来自C#中的Json?,c#,json,C#,Json,我需要从这个JSON格式中获取“temp”、名称和描述。我可以把其中的一部分拿出来,但不是全部。我把问题都问了一遍,但没有得到具体的答案 HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=XXX")); WebReq.Method = "GET"; H
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=XXX"));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
//modified from your code since the using statement disposes the stream
//automatically when done
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(jsonString);
foreach (var item in obj)
{
}
您需要使用
Json.NET
(NuGet)。然后,为json创建一个模型,您可以使用一些在线工具(如和)来帮助您将json转换为对象。然后,使用带有Json.NET
的模型对其进行反序列化(换句话说,将Json映射到模型)
然后,使用新创建的对象访问json值
下面是一个Json示例(使用):
型号:
public class WeatherCast
{
[JsonProperty("coord")]
public Coord Coord { get; set; }
[JsonProperty("weather")]
public Weather[] Weather { get; set; }
[JsonProperty("base")]
public string Base { get; set; }
[JsonProperty("main")]
public Main Main { get; set; }
[JsonProperty("visibility")]
public long Visibility { get; set; }
[JsonProperty("wind")]
public Wind Wind { get; set; }
[JsonProperty("clouds")]
public Clouds Clouds { get; set; }
[JsonProperty("dt")]
public long Dt { get; set; }
[JsonProperty("sys")]
public Sys Sys { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("cod")]
public long Cod { get; set; }
}
public class Weather
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("main")]
public string Main { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("icon")]
public string Icon { get; set; }
}
public class Clouds
{
[JsonProperty("all")]
public long All { get; set; }
}
public class Coord
{
[JsonProperty("lon")]
public double Lon { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
}
public class Main
{
[JsonProperty("temp")]
public double Temp { get; set; }
[JsonProperty("pressure")]
public long Pressure { get; set; }
[JsonProperty("humidity")]
public long Humidity { get; set; }
[JsonProperty("temp_min")]
public double TempMin { get; set; }
[JsonProperty("temp_max")]
public double TempMax { get; set; }
}
public class Sys
{
[JsonProperty("type")]
public long Type { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("message")]
public double Message { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("sunrise")]
public long Sunrise { get; set; }
[JsonProperty("sunset")]
public long Sunset { get; set; }
}
public class Wind
{
[JsonProperty("speed")]
public double Speed { get; set; }
[JsonProperty("deg")]
public long Deg { get; set; }
}
然后,使用Json.NET执行以下操作:
var json = "{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":300,\"main\":\"Drizzle\",\"description\":\"light intensity drizzle\",\"icon\":\"09d\"}],\"base\":\"stations\",\"main\":{\"temp\":280.32,\"pressure\":1012,\"humidity\":81,\"temp_min\":279.15,\"temp_max\":281.15},\"visibility\":10000,\"wind\":{\"speed\":4.1,\"deg\":80},\"clouds\":{\"all\":90},\"dt\":1485789600,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.0103,\"country\":\"GB\",\"sunrise\":1485762037,\"sunset\":1485794875},\"id\":2643743,\"name\":\"London\",\"cod\":200}";
var forecast = JsonConvert.DeserializeObject<WeatherCast>(json);
var temp = forecast.Main.Temp;
var name = forecast.Name;
var description = forecast.Weather[0].Description;
目前,51.51},“天气”方面:““天气”方面:““天气”方面:”,,,“天气”方面:“,,,,,,,,,,,,,,,,,“主要”“主要”“主”“主”“主”“主”“主”“主”以下:300,,,,,“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“主”“压”“压力”“压力”:1012,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“速度\”:4.1,\“度\”:80},\”云“:{all\:90},\'dt\:1485789600,\'sys\:{'type\:1,\'id\:5091,\'message\:0.0103,\'country\:'GB\,'sunrise\:1485762037,\'sunset\:1485794875},\'id\:264374743,\'name\:'London\,'cod\:200}”;
var forecast=JsonConvert.DeserializeObject(json);
var temp=预测主温度;
var name=forecast.name;
var description=forecast.Weather[0]。description;
我希望您更改应用程序id。。。有人可以使用它,也可以滥用它。永远不要发布这些东西。这是一个样本,不是我的,但谢谢你,EdPlunkett,也不能在这里发布long@mohamad请删除
json
评论,我已经更新了你的帖子。顺便说一句,很抱歉搞得一团糟,我只是个新手。
var json = "{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":300,\"main\":\"Drizzle\",\"description\":\"light intensity drizzle\",\"icon\":\"09d\"}],\"base\":\"stations\",\"main\":{\"temp\":280.32,\"pressure\":1012,\"humidity\":81,\"temp_min\":279.15,\"temp_max\":281.15},\"visibility\":10000,\"wind\":{\"speed\":4.1,\"deg\":80},\"clouds\":{\"all\":90},\"dt\":1485789600,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.0103,\"country\":\"GB\",\"sunrise\":1485762037,\"sunset\":1485794875},\"id\":2643743,\"name\":\"London\",\"cod\":200}";
var forecast = JsonConvert.DeserializeObject<WeatherCast>(json);
var temp = forecast.Main.Temp;
var name = forecast.Name;
var description = forecast.Weather[0].Description;