0

//Edit: This isn't a duplicate the suggested thread isn't helping me anyway, because my question is relating to the Newton.Json// I'm using the Newtonsoft.Json for my historical Weatherapp (univerity project). So I chose to use openweathermap.org. Here my code:

    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>();

I'm always geting an exception: NullReferenceException: Object Reference is not set to an instance of an object. Additional info: json and rawjson is not null, temp_value is. I'm sorry when I asked the question in worng place. I'm new here folks.

PS: the uri is not completly correct, I replaced my town to exampletown

John Slegers
  • 45,213
  • 22
  • 199
  • 169
ad0R
  • 35
  • 1
  • 6
  • It would be helpful to see what the json string looks like. – cgotberg Jan 14 '15 at 18:19
  • Hey. Try to use this URI: http://api.openweathermap.org/data/2.5/history/city?id=2885679&type=hour&start=1369728000&end=1369789200&type=hour – ad0R Jan 15 '15 at 10:43
  • Where does the exception occur? If it is in the `json["temp"]` line, just check that `json["temp"]` isn't null before trying to access it. This isn't related to Newtonsoft.Json, this is really a duplicate. If you want to know why `json["temp"]` is null, isolate the json snippet that's causing the problem. You'll never find the problem if you try to parse all the data at once, and neither can we – Panagiotis Kanavos Jan 15 '15 at 11:20
  • This isn't relating anymore to the null reference. The problem was that the keyword "temp" isn't a JSON property. But I only need to get the temps out of the json file. So I'm trying to ask what I can do to get this data with newton.json – ad0R Jan 15 '15 at 12:15

1 Answers1

0

It might be because the JSON being returned does not have a property called temp. For example I tried the following api call in Fiddler 1http://api.openweathermap.org/data/2.5/history/city?id=2885679&type=hour&start=1369728000&end=1369789200&type=hour and do not see a temp property.

Json being returned

Lukkha Coder
  • 4,421
  • 29
  • 25
  • Oh I see. So I tried city_id and everthing is fine. But this is not the result I was looking for. I really need to get something like this: "temp":278.241,"temp_min":278.241,"temp_max":278.241. When I'm calling the list attribute, the value is still null. Any ideas? – ad0R Jan 14 '15 at 18:40
  • That could be because you are trying to cast it as a string when you write `.ToObject();` – Lukkha Coder Jan 14 '15 at 18:52
  • What would you recommend? – ad0R Jan 14 '15 at 19:57