user3839613 user3839613 - 5 months ago 18
Java Question

String to Json object to Json Array in Java

Need to convert below json response to Json object and then "update" field should be converted to json array.
Basically needs to fetch each key, value pair.

{
"Response": [
{
"TicketID": "200",
"update": [
{
"Type": "Group",
"user": "ABC",
"groupName": "RTT",
"updatetime": "1465367505863"
},
{
"Type": "status",
"user": "ABC",
"status": "Open",
"updatetime": "1465367505858"
},
{
"Type": "comment",
"user": "ABC",
"comments": "Updating ",
"updatetime": "1465367505854"
}
]
},
{
"TicketID": "300",
"update": [
{
"Type": "Group",
"user": "ABC",
"groupName": "RTT",
"updatetime": "1465367505863"
},
{
"Type": "status",
"user": "ABC",
"status": "Open",
"updatetime": "1465367505854"
},
{
"Type": "comment",
"user": "ABC",
"comments": "Updating Group",
"updatetime": "1465367505834"
}
]
}
]
}


I have tried below code :-

InputStream is = new ByteArrayInputStream(ticket.getBytes());
// create json reader from json
JsonReader reader = Json.createReader(is);

// get the jsonobject structure from JsonReader
JsonObject ticObj = reader.readObject();

JsonArray arrTicket = ticObj.getJsonArray("update");
String tktid=ticObj.getString("TicketID");
System.out.println("Ticket id is "+tktid);

System.out.println("ticket Id =>");

for (javax.json.JsonValue value : arrTicket) {
String ticketid = value.toString();
System.out.print(" " + ticketid + "\n");

}


Getting below error as "javax.json.stream.JsonParsingException: Unexpected char 84 at (line no=1, column no=16, offset=15)"

Answer

DO it like this:

try {
        JSONObject jsonObject = new JSONObject("{" +
                "\"Response\": [" +
                "    {" +
                "        \"TicketID\": \"200\"," +
                "        \"update\": [" +
                "            {" +
                "                \"Type\": \"Group\"," +
                "                \"user\": \"ABC\"," +
                "                \"groupName\": \"RTT\"," +
                "                \"updatetime\": \"1465367505863\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"status\"," +
                "                \"user\": \"ABC\"," +
                "                \"status\": \"Open\"," +
                "                \"updatetime\": \"1465367505858\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"comment\"," +
                "                \"user\": \"ABC\"," +
                "                \"comments\": \"Updating \"," +
                "                \"updatetime\": \"1465367505854\"" +
                "            }" +
                "        ]" +
                "    }," +
                "    {" +
                "        \"TicketID\": \"300\"," +
                "        \"update\": [" +
                "            {" +
                "                \"Type\": \"Group\"," +
                "                \"user\": \"ABC\"," +
                "                \"groupName\": \"RTT\"," +
                "                \"updatetime\": \"1465367505863\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"status\"," +
                "                \"user\": \"ABC\"," +
                "                \"status\": \"Open\"," +
                "                \"updatetime\": \"1465367505854\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"comment\"," +
                "                \"user\": \"ABC\"," +
                "                \"comments\": \"Updating Group\"," +
                "                \"updatetime\": \"1465367505834\"" +
                "            }" +
                "        ]" +
                "    }" +
                "]" +
                "}");

        JSONArray jsonArray = jsonObject.optJSONArray("Response");

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

            JSONObject object = jsonArray.getJSONObject(i);

            String TicketID = object.optString("TicketID");

            System.out.println("TicketID ----"+TicketID);

            JSONArray update = object.getJSONArray("update");

            for (int j = 0; j < update.length(); j++) {

                JSONObject obj = update.getJSONObject(j);

                String Type = obj.optString("Type");
                System.out.println("Type :"+i+":"+Type);
                String user = obj.optString("user");
                System.out.println("user :"+i+":"+user);
                String groupName = obj.optString("groupName");
                System.out.println("groupName :"+i+":"+groupName);
                String updatetime = obj.optString("updattime");
                System.out.println("updatetime :"+i+":"+updatetime);
            }

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
Comments