2012-05-04 59 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表示根,所以你不能跳過任何節點。