String string = yourjson; JSONObject o = new JSONObject(yourjson); JSONArray a = o.getJSONArray("types"); for (int i = 0; i < a.length(); i++) { Log.d("Type", a.getString(i)); }
That would be correct if you were only parsing the line above. Please note that to access types from the GoogleMaps geocode you should get an array of results, not component_address, then you can access the components.getJSONObject (index) object.
This is a simple implementation that only analyzes formatted_address - what I need in my project.
private void parseJson(List<Address> address, int maxResults, byte[] data) { try { String json = new String(data, "UTF-8"); JSONObject o = new JSONObject(json); String status = o.getString("status"); if (status.equals(STATUS_OK)) { JSONArray a = o.getJSONArray("results"); for (int i = 0; i < maxResults && i < a.length(); i++) { Address current = new Address(Locale.getDefault()); JSONObject item = a.getJSONObject(i); current.setFeatureName(item.getString("formatted_address")); JSONObject location = item.getJSONObject("geometry") .getJSONObject("location"); current.setLatitude(location.getDouble("lat")); current.setLongitude(location.getDouble("lng")); address.add(current); } } catch (Throwable e) { e.printStackTrace(); } }
source share