user2960058
user2960058

Reputation: 61

Traverse JSON data in JAVA

I am new to JSON..Am using HTTPUrlConnections and getting some response in JAVA program.The response data will be like,

    {
    "data": [
        {
    "id": 1,
            "userId": 1,
            "name": "ABC",
            "modified": "2014-12-04",
            "created": "2014-12-04",
            "items": [
                {
                    "email": "[email protected]",
                    "links": [
                        {
                            .
                            .
                            .
                            .
                        }
                    ]
                }
            ]
            }
        ]
}

From this response am able to get the value of "name" field with the below java code.

JSONArray items = newObj.getJSONArray("data");
for (int it=0 ; it < items.length() ; it++){
    JSONObject contactItem = items.getJSONObject(it);
    String userName = contactItem.getString("name");
    System.out.println("Name----------"+userName);
}

But my requirement is,I need to get the value of "email" ..How should I code for that.. Any advice..

Thanks in advance.. Chitra

Upvotes: 6

Views: 11547

Answers (5)

user2960058
user2960058

Reputation: 61

Thank you so much for your time and response. The below code did the magic:

JSONArray responseContactData = responseContact.getJSONArray("data");
for (int i=0; i < responseContactData.length(); i++) {
    String emails = contactDataValues.getJSONArray("items").getJSONObject(0).getString("email");
}

Upvotes: 0

Abhishek
Abhishek

Reputation: 878

If you look the JSON String it contains items which can be considered as an Array within an Array, so in order to get the value of email all you need to do is to create another JSONArray like:

JSONArray itemsArray = contactItem.getJSONArray("items");

Then you can retrieve the value of email over this Array

Upvotes: 0

sol4me
sol4me

Reputation: 15718

You need to first get the items array and each entry of this array contains JSONObject, from which you can call getString("email") .E.g.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class App
{

    public static void main( String[] args ) throws JSONException {
        JSONObject newObj = new JSONObject("{" +
                "\"data\": [\n" +
                "    {\n" +
                "\"id\": 1,\n" +
                "        \"userId\": 1,\n" +
                "        \"name\": \"ABC\",\n" +
                "        \"modified\": \"2014-12-04\",\n" +
                "        \"created\": \"2014-12-04\",\n" +
                "        \"items\": [\n" +
                "            {\n" +
                "                \"email\": \"[email protected]\",\n" +
                "                \"links\": [\n" +
                "                    {\n" +

                "                    }\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "        }\n" +
                "    ]\n" +
                "\n" +
                "}");



        JSONArray items = newObj.getJSONArray("data");
        for (int it = 0; it < items.length(); it++) {
            JSONObject contactItem = items.getJSONObject(it);
            String userName = contactItem.getString("name");


            JSONArray item = contactItem.getJSONArray("items");

            for (int i = 0; i < items.length(); i++) {
                String email = item.getJSONObject(i).getString("email");
                System.out.println(email);
            }

            System.out.println("Name----------" + userName);
        }
    }
}

Output

[email protected]
Name----------ABC

Upvotes: 3

Adil Aliyev
Adil Aliyev

Reputation: 1169

You can also use Jackson library from FasterXML. You can convert the JSON String into Java object very easily and then you can traverse using iterations on Collections.

Upvotes: 0

Himanshu Bhardwaj
Himanshu Bhardwaj

Reputation: 4123

Extending your logic only:

JSONArray items = newObj.getJSONArray("data");
for (int it=0 ; it < items.length() ; it++){
    JSONObject contactItem = items.getJSONObject(it);
    String userName = contactItem.getString("name");
    System.out.println("Name----------"+userName);

    JSONArray itemsArr = contactItem.getJSONArray("items");
    for (int item=0 ; item < itemsArr.length() ; item++){
        String email = item.getString("email");
        System.out.println("Email----------"+email);
    }

}

This should work, with few tweaks. I have not actually tested it, just writing freehand here.

Upvotes: 0

Related Questions