Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traverse JSON data in JAVA

Tags:

java

json

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

like image 766
user2960058 Avatar asked Dec 08 '14 07:12

user2960058


People also ask

How do I traverse a JSON array?

1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.

How do you parse a JSON object in Java?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.

How do I iterate through a JSON object?

Use Object.values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

What is the use of JsonParser in Java?

Interface JsonParser. Provides forward, read-only access to JSON data in a streaming way. This is the most efficient way for reading JSON data. The class Json contains methods to create parsers from input sources ( InputStream and Reader ).


1 Answers

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
like image 50
sol4me Avatar answered Sep 30 '22 20:09

sol4me