我有以下代码段:
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);
}
}
我希望它从预测列表中获取其描述。
但是我收到了这个错误
对象引用未设置为对象的实例
这是我的全部课程清单:
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; }
}
有人知道为什么会出现吗?
答案 0 :(得分:1)
JSON很可能与提供的类定义不匹配,该类定义在解析时会导致对象为空。
在浏览器中调用显示的URL将提供以下JSON响应
{“ coord”:{“ lon”:-0.13,“ lat”:51.51},“ weather”:[{“ id”:300,“ main”:“毛毛雨”,“描述”:“光 强度 下毛毛雨“,” icon“:” 09d“}],” base“:” stations“,” main“:{” temp“:280.32,” pressure“:1012,”湿度“:81,” temp_min“:279.15, “ temp_max”:281.15},“可见性”:10000,“风”:{“速度”:4.1,“度”:80},“云”:{“全部”:90},“ dt”:1485789600,“ sys“:{” type“:1,” id“:5091,” message“:0.0103,” country“:” GB“,” sunrise“:1485762037,” sunset“:1485794875},” id“:2643743,” name“:” London“,” cod“:200}
可以将其映射到已显示的经过一些修改的代码。
public class WeatherForcast {
public List<weather> weather { get; set; }
}
public class weather {
public string main { get; set; }
public string description { get; set; }
}
有一些在线工具可用于放置JSON,它将生成JSON的映射类。
答案 1 :(得分:0)
您可以使用&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; }
}
}