0

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

我尝试过以下示例: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();
4

1 回答 1

0

怎么样:

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

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

于 2012-07-30T22:08:22.630 回答