C# 使用JSON.NET windows phone解析JSON
我试图解析openweathermap中的7天预测。我在foreach(JsonRequest[“main”][“wind”][“name”][“weather”])中的JObject数组的C# 使用JSON.NET windows phone解析JSON,c#,json,json.net,C#,Json,Json.net,我试图解析openweathermap中的7天预测。我在foreach(JsonRequest[“main”][“wind”][“name”][“weather”])中的JObject数组的行中捕捉到一个空引用exeption,但我不明白为什么。我需要填好我的班级,并展示7天的预测 string queryString = "http://api.openweathermap.org/data/2.1/forecast/city?lat=" + latitude + "&lon=" +
行中捕捉到一个空引用exeption
,但我不明白为什么。我需要填好我的班级,并展示7天的预测
string queryString = "http://api.openweathermap.org/data/2.1/forecast/city?lat=" + latitude + "&lon=" + longitude + "?units=metric";
dynamic results = await DataService.getDataFromService(queryString).ConfigureAwait(false);
MyWeather wetherobject = new MyWeather();
PanoramaItemObject ItemObject = new PanoramaItemObject();
var JsonRequest = JObject.Parse(results);
foreach (JObject array in JsonRequest["main"]["wind"]["name"]["weather"])
{
JObject obj = JObject.Parse(array.ToString());
wetherobject.Temperature=(string)obj["temp"];
wetherobject.Humidity=(string)obj["humidity"];
wetherobject.Temperature_max=(string)obj["temp_max"];
wetherobject.Temperature_min=(string)obj["temp_min"];
wetherobject.Title=(string)obj["main"];
wetherobject.Description=(string)obj["description"];
wetherobject.Wind=(string)obj["speed"];
ItemObject.forecasts.Add(wetherobject);
}
层次结构如下:
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class City
{
public int id { get; set; }
public string name { get; set; }
public Coord coord { get; set; }
public string country { get; set; }
public int population { get; set; }
}
public class Main
{
public double temp { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public double pressure { get; set; }
public double sea_level { get; set; }
public double grnd_level { get; set; }
public int humidity { get; set; }
public double temp_kf { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double deg { get; set; }
}
public class Sys
{
public string pod { get; set; }
}
public class Rain
{
public double __invalid_name__3h { get; set; }
}
public class List
{
public int dt { get; set; }
public Main main { get; set; }
public List<Weather> weather { get; set; }
public Clouds clouds { get; set; }
public Wind wind { get; set; }
public Sys sys { get; set; }
public string dt_txt { get; set; }
public Rain rain { get; set; }
}
public class RootObject
{
public string cod { get; set; }
public string message { get; set; }
public City city { get; set; }
public int cnt { get; set; }
public string model { get; set; }
public List<List> list { get; set; }
}
您的对象层次结构不同。下面的例子应该有用
double latitude = 51.5085300;
double longitude = -0.1257400;
string queryString = "http://api.openweathermap.org/data/2.1/forecast/city?lat=" + latitude + "&lon=" + longitude + "?units=metric";
HttpClient client = new HttpClient();
string results = await client.GetStringAsync(queryString).ConfigureAwait(false);
dynamic jobj = JObject.Parse(results);
foreach (var item in jobj.list)
{
Console.WriteLine("{0} TEMP:{1} WIND:{2}",
DateTime.ParseExact((string)item.dt_txt, "yyyy-MM-dd HH:mm:ss", null),
(item.main.temp - 273.15),
item.wind.speed);
}
您的对象层次结构不像
main.wind.name.weather
。将json粘贴到并查看结果。(你也可以使用JsonViewer)同意EZI所说的。还可以尝试发布以可视化您的数据。顺便说一句,当查询一个长的属性序列时,一些中间属性可能会丢失,我建议使用,因为它将返回一个空的枚举,而不是抛出一个空引用异常。dynamic JsonRequest=JObject.Parse(results);foreach(JsonRequest.list中的var数组)现在我得到错误“Newtonsoft.Json.Linq.JObject”不包含“list”的定义@Youhy我发布了一个测试过的工作代码。你能用wetherobject.Temperature作为输出而不是控制台来编写你的示例吗?我已经发布了我的新代码,我不明白出了什么问题
double latitude = 51.5085300;
double longitude = -0.1257400;
string queryString = "http://api.openweathermap.org/data/2.1/forecast/city?lat=" + latitude + "&lon=" + longitude + "?units=metric";
HttpClient client = new HttpClient();
string results = await client.GetStringAsync(queryString).ConfigureAwait(false);
dynamic jobj = JObject.Parse(results);
foreach (var item in jobj.list)
{
Console.WriteLine("{0} TEMP:{1} WIND:{2}",
DateTime.ParseExact((string)item.dt_txt, "yyyy-MM-dd HH:mm:ss", null),
(item.main.temp - 273.15),
item.wind.speed);
}