C# 解析开放天气图API响应失败
我有一段代码:C# 解析开放天气图API响应失败,c#,.net,xml,wpf,object,C#,.net,Xml,Wpf,Object,我有一段代码: public void getForcast() { string url = string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London&appid=b6907d289e10d714a6e88b30761fae22"); using (WebClient web = new WebClient()) { var json = web.Dow
public void getForcast()
{
string url = string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London&appid=b6907d289e10d714a6e88b30761fae22");
using (WebClient web = new WebClient())
{
var json = web.DownloadString(url);
var obj = JsonConvert.DeserializeObject<WeatherData.WeatherForcast>(json);
WeatherData.WeatherForcast forcast = obj;
WeatherMark.Text = string.Format("{0}", forcast.list[1].weathers[0].description);
}
}
public void getForcast()
{
字符串url=string.Format(“https://samples.openweathermap.org/data/2.5/weather?q=London&appid=b6907d289e10d714a6e88b30761fae22");
使用(WebClient web=new WebClient())
{
var json=web.DownloadString(url);
var obj=JsonConvert.DeserializeObject(json);
WeatherData.WeatherForcast forcast=obj;
WeatherMark.Text=string.Format(“{0}”,forcast.list[1]。weathers[0]。description);
}
}
我想从预测列表中得到它的描述
但是我却得到了这个错误
对象引用未设置为对象的实例
以下是我的全部课程列表:
class WeatherForcast
{
public List<list> list { get; set; }
}
public class weather
{
public string main { get; set; }
public string description { get; set; }
}
public class list
{
public List<weather> weathers { get; set; }
}
class天气预报
{
公共列表{get;set;}
}
公共天气
{
公共字符串main{get;set;}
公共字符串说明{get;set;}
}
公共班级名单
{
公共列表天气{get;set;}
}
有人知道它为什么会出现吗?JSON很可能与提供的类定义不匹配,解析时会导致空对象 在浏览器中调用显示的URL将提供以下JSON响应 {“coord”:{“lon”:-0.13,“lat”:51.51},“weather”:[{“id”:300,“main”:“毛毛雨”,“description”:“光线” 强烈 毛毛雨,“图标”:“09d”}],“基地”:“车站”,“主要”:{“温度”:280.32,“气压”:1012,“湿度”:81,“温度”:279.15,“温度”:281.15},“能见度”:10000,“风”:{“速度”:4.1,“度”:80},“云”:{“所有”:90},“dt”:1485789600,“系统”:{“类型”:1,“身份证”:5091,“信息”:0.0103,“国家”:“国标”,“日出”:1485762037,“日落”:1485794875},“身份证”:26437,“伦敦”:“姓名”:“伦敦”:200} 它可以映射到您已经提供了一些修改的代码
public class WeatherForcast {
public List<weather> weather { get; set; }
}
public class weather {
public string main { get; set; }
public string description { get; set; }
}
公共类天气预报{
公共天气列表{get;set;}
}
公共天气{
公共字符串main{get;set;}
公共字符串说明{get;set;}
}
有在线工具可用于放置JSON,它将为JSON生成映射类。您可以使用&mode=Xml获取Xml中的天气信息,然后使用Xml序列化:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string URL = "https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(URL);
XmlSerializer serializer = new XmlSerializer(typeof(Weather));
Weather weather = (Weather)serializer.Deserialize(reader);
}
}
[XmlRoot("current")]
public class Weather
{
public City city { get; set; }
public Temperature temperature { get; set; }
public Humidity humidity { get; set; }
public Pressure pressure { get; set; }
public Wind wind { get; set; }
}
public class City
{
[XmlAttribute()]
public string name { get; set; }
[XmlAttribute()]
public string id { get; set; }
public Coord coord { get; set; }
public string country { get; set; }
public Sun sun { get; set; }
}
public class Sun
{
[XmlAttribute()]
public DateTime rise { get; set; }
[XmlAttribute()]
public DateTime set { get; set; }
}
public class Coord
{
[XmlAttribute()]
public decimal lon { get; set; }
[XmlAttribute()]
public decimal lat { get; set; }
}
public class Temperature
{
[XmlAttribute()]
public decimal value { get; set; }
[XmlAttribute()]
public decimal min { get; set; }
[XmlAttribute()]
public decimal max { get; set; }
}
public class Humidity
{
[XmlAttribute()]
public decimal value { get; set; }
}
public class Pressure
{
[XmlAttribute()]
public decimal value { get; set; }
}
public class Wind
{
public Speed speed { get; set; }
public Direction direction { get; set; }
}
public class Speed
{
[XmlAttribute()]
public decimal value { get; set; }
[XmlAttribute()]
public string name { get; set; }
}
public class Direction
{
[XmlAttribute()]
public decimal value { get; set; }
[XmlAttribute()]
public string name { get; set; }
[XmlAttribute()]
public string code { get; set; }
}
}
JSON很可能与提供的类定义不匹配,这会在解析时导致空对象。您是否尝试调试代码?下载json包含什么?你到底是从哪里得到错误的?