JSON response value looks like this "types" : [ "sublocality", "political" ]
. How to get the first value of the types or how to get the word sublocality?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show json array. Step 3 − Add the following code to src/MainActivity.java
This example demonstrate about How to read volley json array in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show json array.
Android JSONObject is used for JSON parsing in android apps. In this tutorial we’ll discuss and implement a JSONObject in our android application to parse JSON data. JSON stands for JavaScript Object Notation. What is JSON? JSON is used for data interchange (posting and retrieving) from the server.
This example demonstrates how to iterate a JSON Array in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.kt
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));
}
This would be correct if you were parsing only the line you provided above. Note that to access types from GoogleMaps geocode you should get an array of results, than address_components, then you can access object components.getJSONObject(index).
This is a simple implementation that parses only formatted_address - what I needed 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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With