0

Unable to convert a JSON string which has few elements and an array inside it. On UI I need to feed the array to bootstrap table.

JSON string:

 {
    "IsOperationSuccessful": true,
    "IsResult": true,
    "StatusCode": "OK",
    "Response": [{
        "PlaylistCreatedBy": "XYZ",
        "PlaylistCreatedOn": "10/10/2019 14:10",
        "PlaylistDisplayTitle": "blah",
        "PlaylistId": 101,
        "PlaylistScheduledReleaseTime": "10/10/2019 14:10"
    }, {
        "PlaylistCreatedBy": "HHJK",
        "PlaylistCreatedOn": "10/10/2019 14:10",
        "PlaylistDisplayTitle": "blah blah",
        "PlaylistId": 102,
        "PlaylistScheduledReleaseTime": "10/10/2019 14:10"
    }, {
        "PlaylistCreatedBy": "HJHJ",
        "PlaylistCreatedOn": "10/10/2019 14:10",
        "PlaylistDisplayTitle": "UIUI",
        "PlaylistId": 103,
        "PlaylistScheduledReleaseTime": "10/10/2019 14:10"
    }, {
        "PlaylistCreatedBy": "KJK",
        "PlaylistCreatedOn": "10/10/2019 14:10",
        "PlaylistDisplayTitle": "kkj",
        "PlaylistId": 104,
        "PlaylistScheduledReleaseTime": "10/10/2019 14:10"
    }],
    "Total": 4
}

The code that I have added so far:

    PreRecordedCall morningCallResponse = new PreRecordedCall();
    JSONArray playListinfo = null; 
    String testResponse = "//Json Goes here "
    JSONObject finalJson = new JSONObject();
    finalJson.put("testResponse", testResponse); 
    Gson gson = new Gson();

    morningCallResponse = gson.fromJson(testResponse, 
    PreRecordedCall.class); 
    playListinfo =  morningCallResponse.getPreRecordplayListInformation(); 
0

2 Answers 2

1

One way is to create 2 POJOs (says Response and PreRecordedCall) as follows. The Response is for the JSON array with key "Response" and the PreRecordedCall is for the whole JSON object. The key is to use List<Response> to store JSON array.

BTW, to follow the naming rule for variables in POJOs with lowerCamelCase, I used @SerializedName for object name mapping.

Class Response

static class Response {
    @SerializedName("PlaylistCreatedBy")
    private String playlistCreatedBy;

    @SerializedName("PlaylistCreatedOn")
    private String playlistCreatedOn;

    @SerializedName("PlaylistDisplayTitle")
    private String playlistDisplayTitle;

    @SerializedName("PlaylistId")
    private int playlistId;

    @SerializedName("PlaylistScheduledReleaseTime")
    private String playlistScheduledReleaseTime;

    //general getters and setters
}

Class PreRecordedCall

static class PreRecordedCall {
    @SerializedName("IsOperationSuccessful")
    private Boolean isOperationSuccessful;

    @SerializedName("IsResult")
    private Boolean isResult;

    @SerializedName("StatusCode")
    private String statusCode;

    @SerializedName("Response")
    private List<Response> response;

    @SerializedName("Total")
    private int total;

    //general getters and setters
}

Then you can simply convert the JSON string to pre-defined POJOs as follows:

Gson gson = new Gson();
PreRecordedCall preRecordedCall = gson.fromJson(testResponse, PreRecordedCall.class);
System.out.println(preRecordedCall.getResponse().size());

Console output

4

And if you are using Jackson as your JSON parser, change @SerializedName to @JsonProperty and you can get the same result by following code snippet.

ObjectMapper mapper = new ObjectMapper();
PreRecordedCall preRecordedCall = mapper.readValue(testResponse, PreRecordedCall.class);
System.out.println(preRecordedCall.getResponse().size());
Sign up to request clarification or add additional context in comments.

Comments

0

Check if the below code works for you:

String finalJson= "//Json Goes here ";
ObjectMapper objectMapper = new ObjectMapper();
try {
    PreRecordedCall morningCallResponse = objectMapper.readValue(json, PreRecordedCall.class);
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

1 Comment

This works fine, but my response needs to be JSONArray not a List<Response> because I need to feed this response to a Bootstrap table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.