最简单的解析JSON字符串的方法 [英] simplest way to parse JSON string
本文介绍了最简单的解析JSON字符串的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何可以浏览JSON字符串从一个键,另一个嵌套键和获得的价值?我有以下字符串
{数据:{current_condition:[{cloudcover:75,
湿度:29,
observation_time:下午7点59分,
precipMM:0.0,
pressure:1011,
TEMP_C:19,
temp_F:67,
可见性:16,
天气code:116,
weatherDesc:[{值:晴间多云}],
weatherIconUrl:[{值:http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png}],
winddir16Point:N,
winddirDegree:350,
windspeedKmph:26,
windspeedMiles:16
}],
要求:[{查询:01801,
类型:拉链code
}],
天气预报:[{日期:2011-05-09,
precipMM:0.0,
tempMaxC:19,
tempMaxF:65,
tempMinC:10,
tempMinF:50,
天气code:113,
weatherDesc:[{值:艳阳天}],
weatherIconUrl:[{值:的http://www/images/wsymbols01_png_64/wsymbol_0001_sunny.png}],
winddir16Point:西北偏北,
winddirDegree:348,
winddirection:西北偏北,
windspeedKmph:24,
windspeedMiles:15
},
{日期:2011-05-10,
precipMM:0.1,
tempMaxC:13,
tempMaxF:56,
tempMinC:12,
tempMinF:53,
天气code:122,
weatherDesc:[{值:雷雨}],
weatherIconUrl:[{值:的http://www/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png}],
winddir16Point:东北偏北,
winddirDegree:12,
winddirection:东北偏北,
windspeedKmph:31,
windspeedMiles:19
}
]
}}
所以,我回答我的问题: 如果别人想快速获得价值:这就是我一直在寻找
的JSONObject J =新的JSONObject(strResponse);
字符串weatherDesc = jObject.getJSONObject("data").getJSONArray("weather").getJSONObject(0).getJSONArray("weatherDesc").getJSONObject(0).getString("value");
解决方案
有在pretty的太多任何语言的JSON库。如果你提出一个,我也许可以给你点的东西。
在此期间,这里有几个:
- Perl的JSON库
- Python的是内置的
- 的Javascript序列化及其在JSON对象所以它的超级容易
- C#库 - 也看到<一href="http://stackoverflow.com/questions/802766/c-parsing-json-formatted-data-into-nested-hashtables">C# - 解析JSON格式的数据转换成嵌套的哈希表
- 下面是Android的JSON参考
等。我提出一个快速谷歌为您所选择的语言。
How can I navigate JSON string from one key to another nested key and get the value? I have the following string
{ "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"
}
]
} }
So I answer my own question: In case someone else want to get value quickly: This is what I was looking for.
JSONObject j = new JSONObject(strResponse);
String weatherDesc = jObject.getJSONObject("data").getJSONArray("weather").getJSONObject(0).getJSONArray("weatherDesc").getJSONObject(0).getString("value");
解决方案
There are JSON libraries in pretty much any language. If you suggest one, I might be able to point you to something.
In the meantime, here are a few:
- Perl JSON library
- Python's is built in
- Javascript serializes its objects in JSON so it's super easy
- C# library -- also see C# - parsing json formatted data into nested hashtables
- Here's the Android JSON reference
And so on. I suggest a quick google for the language of your choice.
这篇关于最简单的解析JSON字符串的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文