Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将包含数组的JSON转换为Java对象_Java_Json_Gson - Fatal编程技术网

如何将包含数组的JSON转换为Java对象

如何将包含数组的JSON转换为Java对象,java,json,gson,Java,Json,Gson,无法转换包含少量元素和数组的JSON字符串。 在UI上,我需要将数组馈送到引导表 JSON字符串: { "IsOperationSuccessful": true, "IsResult": true, "StatusCode": "OK", "Response": [{ "PlaylistCreatedBy": "XYZ", "PlaylistCreatedOn": "10/10/2019 14:10", "Pla

无法转换包含少量元素和数组的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(); 

检查以下代码是否适用于您:

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

一种方法是创建2个POJO(表示
Response
PreRecordedCall
),如下所示。
Response
用于带有键“Response”的JSON数组,
PreRecordedCall
用于整个JSON对象。关键是使用
List
存储JSON数组

顺便说一句,为了遵循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
}
类预录呼叫

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
}
控制台输出

四,

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

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

这很好,但我的响应需要是JSONArray而不是列表,因为我需要将此响应提供给引导表。
ObjectMapper mapper = new ObjectMapper();
PreRecordedCall preRecordedCall = mapper.readValue(testResponse, PreRecordedCall.class);
System.out.println(preRecordedCall.getResponse().size());