How to parse this json.

{ "results" : [ { "address_components" : [ { "long_name" : "45", "short_name" : "45", "types" : [ "street_number" ] }, { "long_name" : "проспект Aль-Фараби", "short_name" : "проспект Aль-Фараби", "types" : [ "route" ] }, { "long_name" : "Бостандыкский район", "short_name" : "Бостандыкский район", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Алматы", "short_name" : "Алматы", "types" : [ "locality", "political" ] }, { "long_name" : "Казахстан", "short_name" : "KZ", "types" : [ "country", "political" ] } ], "formatted_address" : "проспект Aль-Фараби 45, Алматы, Казахстан", "geometry" : { "location" : { "lat" : 43.2249503, "lng" : 76.93780819999999 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 43.22629928029149, "lng" : 76.9391571802915 }, "southwest" : { "lat" : 43.22360131970849, "lng" : 76.93645921970848 } } }, "place_id" : "ChIJkVDDqCNvgzgRXHOe5BavjiE", "types" : [ "street_address" ] }, { "address_components" : [ { "long_name" : "Бостандыкский район", "short_name" : "Бостандыкский район", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Алматы", "short_name" : "Алматы", "types" : [ "locality", "political" ] }, { "long_name" : "Алматинская область", "short_name" : "Алматинская область", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Казахстан", "short_name" : "KZ", "types" : [ "country", "political" ] } ], "formatted_address" : "Бостандыкский район, Алматы, Казахстан", "geometry" : { "bounds" : { "northeast" : { "lat" : 43.2426069, "lng" : 76.99433329999999 }, "southwest" : { "lat" : 43.037215, "lng" : 76.83271409999999 } }, "location" : { "lat" : 43.1641072, "lng" : 76.9297354 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 43.2426069, "lng" : 76.99433329999999 }, "southwest" : { "lat" : 43.037215, "lng" : 76.83271409999999 } } }, "place_id" : "ChIJfe4i2c9ogzgRg1Pdg9Caozc", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "address_components" : [ { "long_name" : "Алматы", "short_name" : "Алматы", "types" : [ "locality", "political" ] }, { "long_name" : "Карасайский район", "short_name" : "Карасайский район", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Алматинская область", "short_name" : "Алматинская область", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Казахстан", "short_name" : "KZ", "types" : [ "country", "political" ] } ], "formatted_address" : "Алматы, Казахстан", "geometry" : { "bounds" : { "northeast" : { "lat" : 43.4057021, "lng" : 77.1467686 }, "southwest" : { "lat" : 43.0287453, "lng" : 76.7415618 } }, "location" : { "lat" : 43.2220146, "lng" : 76.8512485 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 43.4057021, "lng" : 77.1467686 }, "southwest" : { "lat" : 43.0287453, "lng" : 76.7415618 } } }, "place_id" : "ChIJq8vFFn1ugzgRdm2YrY9mRD0", "types" : [ "locality", "political" ] }, { "address_components" : [ { "long_name" : "Казахстан", "short_name" : "KZ", "types" : [ "country", "political" ] } ], "formatted_address" : "Казахстан", "geometry" : { "bounds" : { "northeast" : { "lat" : 55.4419839, "lng" : 87.315415 }, "southwest" : { "lat" : 40.5685841, "lng" : 46.4936719 } }, "location" : { "lat" : 48.019573, "lng" : 66.92368399999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 55.4419839, "lng" : 87.31538399999999 }, "southwest" : { "lat" : 40.5685841, "lng" : 46.493672 } } }, "place_id" : "ChIJR6n87AcQqTgRGw6z5C-Ee18", "types" : [ "country", "political" ] } ], "status" : "OK" } 

I would like to pull the "formatted_address" line from this Json file. It does not work.

I make a request to Google, for decoding, to get the Address of the place where you poked a marker. Here is my code:

 private String getUrl(LatLng address_latlng) { String latlng = address_latlng.latitude + "," + address_latlng.longitude; String parameters = "latlng=" + latlng; String output = "json"; String url = "https://maps.googleapis.com/maps/api/geocode/" + output + "?" + parameters; Log.e("getUrl", ">>>>>>>>>" + url); return url; } private String downloadUrl(String strUrl) throws IOException { String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try { URL url = new URL(strUrl); // Creating an http connection to communicate with url urlConnection = (HttpURLConnection) url.openConnection(); // Connecting to url urlConnection.connect(); // Reading data from url iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } data = sb.toString(); Log.d("downloadUrl", data.toString()); br.close(); } catch (Exception e) { Log.d("Exception", e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } return data; } private class FetchUrl extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... url) { String data = ""; try { // Fetching the data from web service data = downloadUrl(url[0]); Log.d("Background Task data", data.toString()); } catch (Exception e) { Log.d("Background Task", e.toString()); } return data; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); ParserTask parserTask = new ParserTask(); parserTask.execute(result); } } private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { @Override protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { JSONArray arr = null; try { arr = new JSONArray(jsonData[0]); JSONObject jObj = arr.getJSONObject(0); String date = jObj.getString("formatted_address"); Log.d("LOG.D", " " + date); } catch (JSONException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(List<List<HashMap<String, String>>> result) { } } 
  • Do you need only one line of json or not? - Mikhail Vaysman
  • @MikhailVaysman yes - Satanist Devilov
  • then consider the idea of ​​using regular expressions. - Mikhail Vaysman
  • @MikhailVaysman there in the array as I understand it is given three options, I need to somehow get the "formatted_address" from the zero position. - Satanist Devilov
  • one
    Here is an example regex101.com/r/4d9Z8x/2 - Mikhail Vaysman

1 answer 1

Just parse the JSON and get the right value. The simplest option:

 try { JSONObject jsonObject = new JSONObject(data); JSONArray jsonArray = jsonObject.getJSONArray("results"); String address = jsonArray.getJSONObject(0).getString("formatted_address"); Log.e("address", address); } catch (Exception e) { Log.e("ERROR", e.getLocalizedMessage()); } 

Short entry:

 try { String address = new JSONObject(data).getJSONArray("results").getJSONObject(0).getString("formatted_address"); Log.e("address", address); } catch (Exception e) { Log.e("ERROR", e.getLocalizedMessage()); }