java - 如何将具有数组的 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();
解决方案
一种方法是创建 2 个 POJO(例如Response
和PreRecordedCall
),如下所示。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());
推荐阅读
- linux - 获取安装在数组变量 BASH 中的 yum list
- javascript - 点击加载更多数据直到列表长度
- ios - iOS WebKit 无法显示从库或相机中选择的图像
- logging - 在 log4rs 中检索记录器
- python - 根据产品组的创建和交货日期计算订单队列
- python - struct.unpack() struct.error: unpack 需要 124 字节的缓冲区
- node.js - 基于属性值的 Avj 条件模式验证
- python - 如何从 MySQL 获取数据以通过 Facenet 和 MTCNN 显示?
- java - 串口通讯不稳定
- java - 如何使用 IReporter 在 selenium 中获取失败的测试详细信息?