2012-05-04 58 views
0

任何人都可以帮助解决这个问题,无论我尝试什么我无法在网页上显示数据,我可以在JObject o = JObject.Parse(ws);处设置断点并查看数据,但每次尝试我都尝试过结果在不同的错误。我试图做的是获得areaName和国家。无法显示来自json.net的数据

我试图从例子:http://james.newtonking.com/projects/json/help/http://forums.asp.net/t/1780822.aspx/1?How+to+traverse+in+json+没有运气

任何帮助,将不胜感激。 乔治

我的代码如下:

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

回答

0

如何像:

var weatherCity = o["search_api"]["result"][0]["areaName"][0]["value"]; 

o表示根,所以你不能跳过任何节点。