【发布时间】:2011-08-22 00:19:27
【问题描述】:
如何将 JSON 字符串从一个键导航到另一个嵌套键并获取值?我有以下字符串
{ "data" : { "current_condition" : [ { "cloudcover" : "75",
"humidity" : "29",
"observation_time" : "07:59 PM",
"precipMM" : "0.0",
"pressure" : "1011",
"temp_C" : "19",
"temp_F" : "67",
"visibility" : "16",
"weatherCode" : "116",
"weatherDesc" : [ { "value" : "Partly Cloudy" } ],
"weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" } ],
"winddir16Point" : "N",
"winddirDegree" : "350",
"windspeedKmph" : "26",
"windspeedMiles" : "16"
} ],
"request" : [ { "query" : "01801",
"type" : "Zipcode"
} ],
"weather" : [ { "date" : "2011-05-09",
"precipMM" : "0.0",
"tempMaxC" : "19",
"tempMaxF" : "65",
"tempMinC" : "10",
"tempMinF" : "50",
"weatherCode" : "113",
"weatherDesc" : [ { "value" : "Sunny" } ],
"weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0001_sunny.png" } ],
"winddir16Point" : "NNW",
"winddirDegree" : "348",
"winddirection" : "NNW",
"windspeedKmph" : "24",
"windspeedMiles" : "15"
},
{ "date" : "2011-05-10",
"precipMM" : "0.1",
"tempMaxC" : "13",
"tempMaxF" : "56",
"tempMinC" : "12",
"tempMinF" : "53",
"weatherCode" : "122",
"weatherDesc" : [ { "value" : "Overcast" } ],
"weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png" } ],
"winddir16Point" : "NNE",
"winddirDegree" : "12",
"winddirection" : "NNE",
"windspeedKmph" : "31",
"windspeedMiles" : "19"
}
]
} }
所以我回答我自己的问题: 如果其他人想快速获得价值:这就是我想要的。
JSONObject j = new JSONObject(strResponse);
String weatherDesc = jObject.getJSONObject("data").getJSONArray("weather").getJSONObject(0).getJSONArray("weatherDesc").getJSONObject(0).getString("value");
【问题讨论】:
-
您尚未指定您使用的语言。在此页面的中途查看您正在使用的解决方案。 json.org
-
鉴于 OP 的发帖历史,我将押注于 C#。还是你想用 JavaScript 来做这个?
-
@BalusC:但话又说回来,他正在用 .net 编写 Web 服务,还有一个关于在 android 上使用 Web 服务的问题...sooooo...
-
Gazler,我已经点击了其中的两个链接,我期待一些 xpath 导航类型的东西,我在其中放置 xyz/abc/ 并找到值。看来我需要一些时间才能学会这一点。我想获得“数据:”天气“:“日期”:“2011-05-09”:tempMaxC“的价值。其中日期是第一个节点。
-
json-generator.com/j/bQFuLJYZMy?indent=4 我无法解析此链接,它显示的模式与您的 lnk 相似