AndroWaqar
AndroWaqar

Reputation: 231

Get data from nested JSON Object in Java Android

How I can get the "fields" objects 0,1,2,3,4 & only the "name" object string of every object using JSONOBJECT

 [
    {
        "name": "Bank1",
        "fields": {
            "0": {
                "name": "Email",
                "slug": "email",
                "type": "input"
            },
            "1": {
                "name": "City",
                "slug": "city",
                "type": "input"
            },
            "2": {
                "name": "Screenshot",
                "slug": "screenshot",
                "type": "file"
            },
            "3": {
                "name": "Full Name",
                "slug": "full-name",
                "type": "input"
            }
        },
        "status": "Active"
    },
    {
        "name": "Bank2",
        "fields": {
            "0": {
                "name": "Email",
                "slug": "email",
                "type": "input"
            },
            "1": {
                "name": "City",
                "slug": "city",
                "type": "input"
            },
            "2": {
                "name": "Screenshot",
                "slug": "screenshot",
                "type": "file"
            },
            "4": {
                "name": "Submitted Date",
                "slug": "submitted-date",
                "type": "calendar"
            }
        },
        "status": "Active"
    }
]

& this is what I try to done

public void onResponse(String response) {
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                         
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                               
                                String p_name = jsonObject.getString("name");
                                JSONObject jo = jsonObject.getJSONObject("fields");

                                String j1 = jo.getString("0");
                                if (!j1.isEmpty()){
                                    JSONObject jo1 = jo.getJSONObject("0");
                                    String f_name1 = jo1.getString("name");
                                    Log.d("Field1.", f_name1);
                                }
}}catch block...

but the problem is, it gives me value of the object null like [value 4 is null] cuz there is no object for 4 in the first object of fields. please help me solve this prob, appreciate your answers thankyou :)

Upvotes: 0

Views: 1598

Answers (2)

Andrzej Więcławski
Andrzej Więcławski

Reputation: 99

There are some problems with get all keys properly in my IDE/JDK11, so I decided to loop over an ArrayList, basing on @MayurGajra solution, ex:

private static List<List<String>> parseJson(String response) throws JSONException {
    JSONArray jsonArray = new JSONArray(response);
    List<List<String>> result = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        JSONObject jo = jsonObject.getJSONObject("fields");

        List<Object> list = new ArrayList<>();
        jo.keys().forEachRemaining(list::add);
        List<String> subList = new ArrayList<>();

        for (Object o : list) {
            String key;
            if (isString(o))
                key = (String) o;
            else
                continue;
            JSONObject jo1 = jo.getJSONObject(key);
            String f_name1 = jo1.getString("name");
            subList.add(f_name1);
        }
        result.add(subList);
    }
    return result;
}

private static boolean isString(Object o) {
    try {
        String result = (String) o;
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}

The result obtained after processing the above json is as follows:

[[Email, City, Screenshot, Full Name], [Email, City, Screenshot, Submitted Date]]

but it have not to be a List of Lists ;)

-- edit --

To get only first list of elements labeled "name":

    try {
        System.out.println(parseJson(yourJsonAsString).get(0).toString());
    } catch (JSONException e) {
        System.out.println("JSONException:" + e.getMessage());
    }

The result of above is:

[Email, City, Screenshot, Full Name]

Upvotes: 0

Mayur Gajra
Mayur Gajra

Reputation: 9073

You can use keys() iterator of json object & loop on it using while (keys.hasNext())

For your example, it would look something like this:

private void parseJson(String response) {
        try {
            JSONArray jsonArray = new JSONArray(response);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                JSONObject jo = jsonObject.getJSONObject("fields");

                Iterator<String> keys = jo.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    JSONObject jo1 = jo.getJSONObject(key);
                    String f_name1 = jo1.getString("name");
                    Log.d("Field1.", f_name1);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Upvotes: 4

Related Questions