简体   繁体   中英

How to convert JSON which has array to Java objects

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(); 

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());

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM