Can anyone help solve this problem, no matter what i try i cannot display the data on webpage, i can put a breakpoints at JObject o = JObject.Parse(ws); and see the data, but every attempt i have tried results in a different error.I’m trying to do is get the areaName and country.
I have tried the examples from: http://james.newtonking.com/projects/json/help/ and http://forums.asp.net/t/1780822.aspx/1?How+to+traverse+in+json+ with no luck
Any help would be appreciated.
George
My code is below:
string ws = @"{ ""search_api"": { ""result"": [ { ""areaName"": [ {""value"": ""London"" } ], ""country"": [ {""value"": ""United Kingdom"" } ], ""latitude"": ""51.517"", ""longitude"": ""-0.106"", ""population"": ""7421228"", ""region"": [ {""value"": ""City Of London, Greater London"" } ], ""timezone"": [ {""offset"": ""1.0"" } ], ""weatherUrl"": [ {""value"": ""http:\/\/www.worldweatheronline.com\/London-weather\/City-of-London-Greater-London\/GB.aspx"" } ] }, { ""areaName"": [ {""value"": ""London"" } ], ""country"": [ {""value"": ""Canada"" } ], ""latitude"": ""42.983"", ""longitude"": ""-81.250"", ""population"": ""346774"", ""region"": [ {""value"": ""Ontario"" } ], ""timezone"": [ {""offset"": ""-4.0"" } ], ""weatherUrl"": [ {""value"": ""http:\/\/www.worldweatheronline.com\/London-weather\/Ontario\/CA.aspx"" } ] } ] }}";
JObject o = JObject.Parse(ws);
var weatherCity = o["areaName"][0]["value"];
ViewBag.WeatherSearch = weatherCity.ToString();
//var weatherCity = o["search_api"][0]["result"]["areaName"]["value"];
// JArray arr = (JArray)o.SelectToken("search_api");
// JObject cityTown = (JObject)arr[0].SelectToken("result").SelectToken("areaName");
// var weatherCity = cityTown.SelectToken("value");
// IList<string> WeatherCity = o.SelectToken("result").Select(s => (string)s).ToList();
//string WeatherCity = (string)o.SelectToken("result[0].areaName[0].value");
//IList<string> WeatherCities = o["result"].Select(m => (string)m.SelectToken("areaName[0].value")).ToList();
How about something like:
orepresents the root, so you can’t skip any nodes.