Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get json array values in android?

Tags:

json

android

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?

like image 573
Yugesh Avatar asked Feb 05 '13 13:02

Yugesh


People also ask

How to show JSON array in Android Studio?

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

How to read volley JSON array in Android?

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.

What is jsonobject in Android?

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.

How to iterate a JSON array in Android using Kotlin?

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


1 Answers

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();
    }

}
like image 185
Yaroslav Mytkalyk Avatar answered Sep 22 '22 10:09

Yaroslav Mytkalyk