首页 > 解决方案 > 如何将具有数组的 JSON 转换为 Java 对象

问题描述

无法转换其中包含少量元素和数组的 JSON 字符串。在 UI 上,我需要将数组提供给引导表。

JSON字符串:

 {
    "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
}

到目前为止我添加的代码:

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

标签: javajsongson

解决方案


一种方法是创建 2 个 POJO(例如ResponsePreRecordedCall),如下所示。Response用于键为“Response”的 JSON 数组,而用于PreRecordedCall整个 JSON 对象。关键是用来List<Response>存储 JSON 数组。

顺便说一句,为了遵循带有 lowerCamelCase 的 POJO 中变量的命名规则,我使用@SerializedName了对象名称映射。

类响应

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
}

类 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
}

然后您可以简单地将 JSON 字符串转换为预定义的 POJO,如下所示:

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

控制台输出

4

如果您将Jackson其用作 JSON 解析器,请更改@SerializedName@JsonProperty,您可以通过以下代码片段获得相同的结果。

ObjectMapper mapper = new ObjectMapper();
PreRecordedCall preRecordedCall = mapper.readValue(testResponse, PreRecordedCall.class);
System.out.println(preRecordedCall.getResponse().size());

推荐阅读