如何从JSON嵌套数组中检索值?

时间:2013-01-16 18:33:46

标签: android json google-geocoding-api

我在Android应用程序中使用this link。我必须从地址 - 组件JSON数组中检索长名称密钥的值。

以下是JSON结构:

     {
   "results":[
      {
         "address_components":[
            {
               "long_name":"285",
               "short_name":"285",
               "types":[
                  "street_number"
               ]
            },
            {
               "long_name":"Bedford Avenue",
               "short_name":"Bedford Ave",
               "types":[
                  "route"
               ]
            },
            {
               "long_name":"Williamsburg",
               "short_name":"Williamsburg",
               "types":[
                  "neighborhood",
                  "political"
               ]
            },
            {
               "long_name":"Brooklyn",
               "short_name":"Brooklyn",
               "types":[
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"New York",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"Kings",
               "short_name":"Kings",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            },
            {
               "long_name":"11211",
               "short_name":"11211",
               "types":[
                  "postal_code"
               ]
            }
         ],
         "formatted_address":"285 Bedford Avenue, Brooklyn, NY 11211, USA",
         "geometry":{
            "location":{
               "lat":40.71412890,
               "lng":-73.96140740
            },
            "location_type":"ROOFTOP",
            "viewport":{
               "northeast":{
                  "lat":40.71547788029149,
                  "lng":-73.96005841970849
               },
               "southwest":{
                  "lat":40.71277991970850,
                  "lng":-73.96275638029151
               }
            }
         },
         "types":[
            "street_address"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Grand St - Bedford Av",
               "short_name":"Grand St - Bedford Av",
               "types":[
                  "bus_station",
                  "transit_station",
                  "establishment"
               ]
            },
            {
               "long_name":"Williamsburg",
               "short_name":"Williamsburg",
               "types":[
                  "neighborhood",
                  "political"
               ]
            },
            {
               "long_name":"Brooklyn",
               "short_name":"Brooklyn",
               "types":[
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"Kings",
               "short_name":"Kings",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            },
            {
               "long_name":"11211",
               "short_name":"11211",
               "types":[
                  "postal_code"
               ]
            }
         ],
         "formatted_address":"Grand St - Bedford Av, Brooklyn, NY 11211, USA",
         "geometry":{
            "location":{
               "lat":40.7143210,
               "lng":-73.9611510
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.71566998029149,
                  "lng":-73.95980201970849
               },
               "southwest":{
                  "lat":40.71297201970850,
                  "lng":-73.96249998029151
               }
            }
         },
         "types":[
            "bus_station",
            "transit_station",
            "establishment"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Grand St - Bedford Av",
               "short_name":"Grand St - Bedford Av",
               "types":[
                  "bus_station",
                  "transit_station",
                  "establishment"
               ]
            },
            {
               "long_name":"Williamsburg",
               "short_name":"Williamsburg",
               "types":[
                  "neighborhood",
                  "political"
               ]
            },
            {
               "long_name":"Brooklyn",
               "short_name":"Brooklyn",
               "types":[
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"Kings",
               "short_name":"Kings",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            },
            {
               "long_name":"11211",
               "short_name":"11211",
               "types":[
                  "postal_code"
               ]
            }
         ],
         "formatted_address":"Grand St - Bedford Av, Brooklyn, NY 11211, USA",
         "geometry":{
            "location":{
               "lat":40.7146840,
               "lng":-73.9615630
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.71603298029149,
                  "lng":-73.96021401970850
               },
               "southwest":{
                  "lat":40.71333501970850,
                  "lng":-73.96291198029151
               }
            }
         },
         "types":[
            "bus_station",
            "transit_station",
            "establishment"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Bedford Av - Grand St",
               "short_name":"Bedford Av - Grand St",
               "types":[
                  "bus_station",
                  "transit_station",
                  "establishment"
               ]
            },
            {
               "long_name":"Williamsburg",
               "short_name":"Williamsburg",
               "types":[
                  "neighborhood",
                  "political"
               ]
            },
            {
               "long_name":"Brooklyn",
               "short_name":"Brooklyn",
               "types":[
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"Kings",
               "short_name":"Kings",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            },
            {
               "long_name":"11211",
               "short_name":"11211",
               "types":[
                  "postal_code"
               ]
            }
         ],
         "formatted_address":"Bedford Av - Grand St, Brooklyn, NY 11211, USA",
         "geometry":{
            "location":{
               "lat":40.714710,
               "lng":-73.9609990
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.71605898029150,
                  "lng":-73.95965001970849
               },
               "southwest":{
                  "lat":40.71336101970850,
                  "lng":-73.96234798029150
               }
            }
         },
         "types":[
            "bus_station",
            "transit_station",
            "establishment"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Williamsburg",
               "short_name":"Williamsburg",
               "types":[
                  "neighborhood",
                  "political"
               ]
            },
            {
               "long_name":"Brooklyn",
               "short_name":"Brooklyn",
               "types":[
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"New York",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"Kings",
               "short_name":"Kings",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"Williamsburg, Brooklyn, NY, USA",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":40.72517730,
                  "lng":-73.9364980
               },
               "southwest":{
                  "lat":40.69793290,
                  "lng":-73.96984499999999
               }
            },
            "location":{
               "lat":40.70644610,
               "lng":-73.95361629999999
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.72517730,
                  "lng":-73.9364980
               },
               "southwest":{
                  "lat":40.69793290,
                  "lng":-73.96984499999999
               }
            }
         },
         "types":[
            "neighborhood",
            "political"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"11211",
               "short_name":"11211",
               "types":[
                  "postal_code"
               ]
            },
            {
               "long_name":"Brooklyn",
               "short_name":"Brooklyn",
               "types":[
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"New York",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"Brooklyn, NY 11211, USA",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":40.72800890,
                  "lng":-73.92072990
               },
               "southwest":{
                  "lat":40.69763590,
                  "lng":-73.97616690
               }
            },
            "location":{
               "lat":40.71800360,
               "lng":-73.96537150000002
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.72800890,
                  "lng":-73.92072990
               },
               "southwest":{
                  "lat":40.69763590,
                  "lng":-73.97616690
               }
            }
         },
         "types":[
            "postal_code"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Kings",
               "short_name":"Kings",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"Kings, NY, USA",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":40.7394460,
                  "lng":-73.83336509999999
               },
               "southwest":{
                  "lat":40.56956370,
                  "lng":-74.04397190
               }
            },
            "location":{
               "lat":40.65287620,
               "lng":-73.95949399999999
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.7394460,
                  "lng":-73.83336509999999
               },
               "southwest":{
                  "lat":40.56956370,
                  "lng":-74.04397190
               }
            }
         },
         "types":[
            "administrative_area_level_2",
            "political"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Brooklyn",
               "short_name":"Brooklyn",
               "types":[
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"New York",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"Kings",
               "short_name":"Kings",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"Brooklyn, NY, USA",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":40.7394460,
                  "lng":-73.83336509999999
               },
               "southwest":{
                  "lat":40.5510420,
                  "lng":-74.056630
               }
            },
            "location":{
               "lat":40.650,
               "lng":-73.950
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.7394460,
                  "lng":-73.83336509999999
               },
               "southwest":{
                  "lat":40.5510420,
                  "lng":-74.056630
               }
            }
         },
         "types":[
            "sublocality",
            "political"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"New York",
               "short_name":"New York",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"New York, NY, USA",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":40.91524140,
                  "lng":-73.70027209999999
               },
               "southwest":{
                  "lat":40.4959080,
                  "lng":-74.25908790
               }
            },
            "location":{
               "lat":40.71435280,
               "lng":-74.00597309999999
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":40.91524140,
                  "lng":-73.70027209999999
               },
               "southwest":{
                  "lat":40.4959080,
                  "lng":-74.25573489999999
               }
            }
         },
         "types":[
            "locality",
            "political"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"New York",
               "short_name":"NY",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"New York, USA",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":45.0158650,
                  "lng":-71.85626990
               },
               "southwest":{
                  "lat":40.4959080,
                  "lng":-79.76214379999999
               }
            },
            "location":{
               "lat":43.29942850,
               "lng":-74.21793260000001
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":45.01261130,
                  "lng":-71.85626990
               },
               "southwest":{
                  "lat":40.4959080,
                  "lng":-79.76214379999999
               }
            }
         },
         "types":[
            "administrative_area_level_1",
            "political"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"United States",
               "short_name":"US",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"United States",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":71.3898880,
                  "lng":-66.94976079999999
               },
               "southwest":{
                  "lat":18.91106420,
                  "lng":172.45469660
               }
            },
            "location":{
               "lat":37.090240,
               "lng":-95.7128910
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":49.380,
                  "lng":-66.940
               },
               "southwest":{
                  "lat":25.820,
                  "lng":-124.390
               }
            }
         },
         "types":[
            "country",
            "political"
         ]
      }
   ],
   "status":"OK"
}

我的Java源代码:

Source Code with Output Link

我要去印度,但我必须在输出中打印 Vasant Vihar

任何人都可以帮我一些代码来实现这个目标吗?

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:4)

我宁愿建议使用GSON来解析bean。您可以选择感兴趣的字段。

以下是我建议你使用的豆子:

响应类:

public class Response {
private Result [] results;
// getters and setters(optional)
}

结果类:

public class Result{
private AddressComponent[] addressComponents;
// getters and setters(optional)
}

地址组件类:

public class AddressComponent {
private String longName;
private String shortName;
private String [] types;
// getters and setters (optional)
}

以下是你解析的方式:

String str = "<the ugly json you pasted>";
Gson gson = new GsonBuilder()
     .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES )
     .create();

GSON默认忽略所有没有相应Java字段的json属性。

响应响应= gson.fromJson(str,Response .class);

您可以访问感兴趣的领域:

AddressComponent [] AddressComponents = response.getResults()[1].getAddressComponent();

从现在开始,您可以根据需要使用该对象。

答案 1 :(得分:2)

从当前json String获取long_name

JSONObject json=new JSONObject("YOUR_JSON_STRING");

 JSONArray ja = json.getJSONArray("results");

for (int i = 0; i < ja.length(); i++) {

  JSONObject jo = ja.getJSONObject(i);

  JSONArray jaa = jo.getJSONArray("address_components");
  for (int j = 0; j < jaa.length(); j++) {

    JSONObject jotwo = jaa.getJSONObject(j);

       //long_name
       String str_long_name=jotwo.getString("long_name");
   }

}