从嵌套的 JSON 结果中获取 JSON 对象
Get JSON Object from a nested JSON result
我有以下 JSON 结果:
这是天气结果。
我的目标是首先获取城市名称。
然后根据列表中的一个城市,请求一个属性
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"hourly": 1
,
"lang": 1
}
, "results": [
{
"name": "Al-Arz",
"city": "Al-Arz",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40105",
"l": "/q/zmw:00000.1.40105"
}
,
{
"name": "Beirut",
"city": "Beirut",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40100",
"l": "/q/zmw:00000.1.40100"
}
,
{
"name": "Dahr Baidar",
"city": "Dahr Baidar",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40110",
"l": "/q/zmw:00000.1.40110"
}
,
{
"name": "Houche-Al-Oumara",
"city": "Houche-Al-Oumara",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40101",
"l": "/q/zmw:00000.1.40101"
}
,
{
"name": "Merdjayoun",
"city": "Merdjayoun",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40104",
"l": "/q/zmw:00000.1.40104"
}
,
{
"name": "Rayack",
"city": "Rayack",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40102",
"l": "/q/zmw:00000.1.40102"
}
,
{
"name": "Tripoli",
"city": "Tripoli",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40103",
"l": "/q/zmw:00000.1.40103"
}
]
}
}
如何获取所有城市的名称?
提前致谢。
您可以将其视为一个 json 对象。按照这些步骤
将其放入 json 对象中。
然后使用 getString() 方法将 "results" 的值放入一个数组中。
- 然后将此结果转换为 json 数组。
- 然后使用 getString 方法逐个广告遍历 json 数组,您可以获得所有城市的值。
JSONObject rootObject = (JSONObject)new JSONTokener(yourJsonString).nextValue();
JSONObject responseObject = rootObject.getJSONObject("response");
JSONArray cityArray = responseObject.getJSONArray("results");
List<String> listWithCityNames = new ArrayList<String>();
for(int i = 0; i< cityArray.lenght();i++){
listWithCityNames.add(cityArray.getJSONObject(i).getString("name"));
}
for(String city:listWithCityNames){
System.out.println(city);
}
此代码段解析 var yourJsonString
中的 json 字符串,并在您的 response
对象内的 results
数组中收集属性 name
json。如果需要,添加 Try/Catch
个块。
我有以下 JSON 结果:
这是天气结果。
我的目标是首先获取城市名称。
然后根据列表中的一个城市,请求一个属性
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"hourly": 1
,
"lang": 1
}
, "results": [
{
"name": "Al-Arz",
"city": "Al-Arz",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40105",
"l": "/q/zmw:00000.1.40105"
}
,
{
"name": "Beirut",
"city": "Beirut",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40100",
"l": "/q/zmw:00000.1.40100"
}
,
{
"name": "Dahr Baidar",
"city": "Dahr Baidar",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40110",
"l": "/q/zmw:00000.1.40110"
}
,
{
"name": "Houche-Al-Oumara",
"city": "Houche-Al-Oumara",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40101",
"l": "/q/zmw:00000.1.40101"
}
,
{
"name": "Merdjayoun",
"city": "Merdjayoun",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40104",
"l": "/q/zmw:00000.1.40104"
}
,
{
"name": "Rayack",
"city": "Rayack",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40102",
"l": "/q/zmw:00000.1.40102"
}
,
{
"name": "Tripoli",
"city": "Tripoli",
"state": "",
"country": "LB",
"country_iso3166":"LB",
"country_name":"Lebanon",
"zmw": "00000.1.40103",
"l": "/q/zmw:00000.1.40103"
}
]
}
}
如何获取所有城市的名称?
提前致谢。
您可以将其视为一个 json 对象。按照这些步骤
将其放入 json 对象中。
然后使用 getString() 方法将 "results" 的值放入一个数组中。
- 然后将此结果转换为 json 数组。
- 然后使用 getString 方法逐个广告遍历 json 数组,您可以获得所有城市的值。
JSONObject rootObject = (JSONObject)new JSONTokener(yourJsonString).nextValue();
JSONObject responseObject = rootObject.getJSONObject("response");
JSONArray cityArray = responseObject.getJSONArray("results");
List<String> listWithCityNames = new ArrayList<String>();
for(int i = 0; i< cityArray.lenght();i++){
listWithCityNames.add(cityArray.getJSONObject(i).getString("name"));
}
for(String city:listWithCityNames){
System.out.println(city);
}
此代码段解析 var yourJsonString
中的 json 字符串,并在您的 response
对象内的 results
数组中收集属性 name
json。如果需要,添加 Try/Catch
个块。