Как правильно парсить данный 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"
}
Хотел бы с данного Json файла вытащить строку "formatted_address". Никак не получается.
Делаю запрос в гугл, для декодирования, чтоб получить Адресс места, куда тыкнул маркером. Вот мой код:
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) {
}
}
Просто разобрать JSON и достать нужное значение. Простейший вариант:
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());
}
Короткая запись:
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());
}
Виртуальный выделенный сервер (VDS) становится отличным выбором
Пишу тест, в котором хочу обработать нажатие на элемент списка recyclerView
Есть Бд оттуда принимаются данные на Jsf но когда в бд записывается запись из утсровта то Jsf страницу надо обнавть чтоб там отобразились изменеия...