//编辑:这不是重复建议的线程无法帮助我,因为我的问题涉及到Newton.Json // 我是使用Newtonsoft.Json作为我的历史Weatherapp(大学项目)。所以我选择使用openweathermap.org。在这里我的代码:Newton.Json:异常:NullReferenceException天气数据请求openweathermap
var request = (HttpWebRequest)WebRequest.Create("http://api.openweathermap.org/data/2.5/history/city?q=exampletown&start=1420135249&end=1421085649&type=hour")
var response = await request.GetResponseAsync();
var rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd();
var json = JObject.Parse(rawJson);
string temp_value = json["temp"].ToObject<string>();
我总是geting一个异常:NullReferenceException:对象引用未设置为对象的实例。其他信息:json和rawjson不是null,temp_value是。当我在worng的地方提问时,我很抱歉。我是新来的人。
PS:URI是不是完全地正确,我取代我镇exampletown
看看json字符串是什么样子会很有帮助。 – cgotberg
嘿。尝试使用此URI:http://api.openweathermap.org/data/2.5/history/city?id=2885679&type=hour&start=1369728000&end=1369789200&type=hour – ad0R
发生异常的位置在哪里?如果它在'json [“temp”]行中,只要在尝试访问它之前检查'json [“temp”]'是否为空。这与Newtonsoft.Json无关,这实际上是重复的。如果你想知道为什么'json [“temp”]'为空,请隔离导致问题的json片段。如果您尝试一次解析所有数据,则永远不会发现问题,我们也不能 –