How to get data from the name field? I can not understand how to move.

{ "response": { "GeoObjectCollection": { "metaDataProperty": { "GeocoderResponseMetaData": { "request": "N52.076005,E23.723004", "found": "8", "results": "1", "Point": { "pos": "23.723004 52.076005" } } }, "featureMember": [ { "GeoObject": { "metaDataProperty": { "GeocoderMetaData": { "kind": "house", "text": "Беларусь, Брест, улица Морозова, 5", "precision": "exact", "AddressDetails": { "Country": { "AddressLine": "Брест, улица Морозова, 5", "CountryNameCode": "BY", "CountryName": "Беларусь", "AdministrativeArea": { "AdministrativeAreaName": "Брестская область", "Locality": { "LocalityName": "Брест", "Thoroughfare": { "ThoroughfareName": "улица Морозова", "Premise": { "PremiseNumber": "5" } } } } } } } }, "description": "Брест, Беларусь", "name": "улица Морозова, 5", "boundedBy": { "Envelope": { "lowerCorner": "23.714535 52.070874", "upperCorner": "23.730992 52.081015" } }, "Point": { "pos": "23.722764 52.075945" } } } ] } } } 
  • Try to use the GSON library to work with JSON, and translate JSON into an object right away, save yourself from all these problems with JSONObject and JSONArray - Werder

1 answer 1

I solved my question with this method:

 String responseData = response.body().string(); JSONObject json = new JSONObject(responseData); JSONObject response_json = json.getJSONObject("response"); JSONObject GeoObjectCollection = response_json.getJSONObject("GeoObjectCollection"); JSONArray featureMember = GeoObjectCollection.getJSONArray("featureMember"); JSONObject featureMember2 = featureMember.getJSONObject(0); JSONObject GeoObject = featureMember2.getJSONObject("GeoObject"); final String owner = GeoObject.getString("name"); 
  • 2
    bad practice is to use hard-coded .get(123) + tsifir indexes in the names of variables that undermine code understanding ( something123 ). if not only the name from the object is important to you, you can use the gson library to compile a convenient POJO - Evgeny Lebedev