Why is this the Json.Net conversion from XML and JSON not working?



You can enter either of these URLs into a browser and verify that they return valid xml:



http://ift.tt/1uejYa3"


...or json:



http://ift.tt/1v7ACpF


...but when attempting to get the xml programmatically using this code (after installing Json.NET via NuGet into my project and dropping a dataGridView on my Windows form):



dataGridView1.DataSource = GetLocationData("http://ift.tt/1v7AF4Q");

private JArray GetLocationData(string uri)
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JArray>(s);
}


...I get: Newtonsoft.Json.JsonReaderException was unhandled _message=Unexpected character encountered while parsing value: <. Path '', line 0, position 0.


I thought, okay, this actually is json code, not xml, so I replaced "xml" with "json" in the URL, expecting more joy in Mudville.


However, when attempting to get the same data as json, I also get an exception, namely, "System.InvalidCastException was unhandled _message=Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'."


Why, and how can I fix it? Is it possible to also grab the data as xml?


No comments:

Post a Comment