无法解析嵌套 json 和更改响应结构
Trouble parsing nested json and changing response structure
我在解析 json 并稍微更改响应结构时遇到问题。下面是我正在尝试解析的 json -
{
"subRecords": [
{
"row": [
{
"practice": [
{
"addressId": "123",
"name":"JON"
}
]
}
],
"Key": "123456-ABC"
}
]
}
我的预期输出是没有 row
-
{
"subRecords": [
{
"practice": [
{
"addressId": "123",
"name": "JON"
}
],
"Key": "123456-ABC"
}
]
}
下面是我的pojo -
@Getter
@Setter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Test{
private Map<String,Object>Key;
private List<Map<String,Object>>practice;
private List<Map<String, Object>> subRecords;
public List<Map<String, Object>> getSubRecords() {
return subRecords;
}
public void setSubRecords(List<Map<String, Object>> row) {
this.practice= (List<Map<String, Object>>) row.get(0).get("practice");
}
}
但我不断收到如下解析错误-
"errors": {
"status": "500",
"title": "Internal Server Error",
"detail": "N/A\n at [Source: (String)\
我不确定我是否正确访问 json 我将不胜感激任何帮助。
要将第一个 JSON 解析为 Java 对象,您需要以下数据模型:
public class Original {
private List<OriginalSubRecords> subRecords;
}
public class OriginalSubRecords {
private OriginalSubRecordsRow row;
private String Key;
}
public class OriginalSubRecordsRow {
private List<OriginalSubRecordsRowPractice> practice;
}
public class OriginalSubRecordsRowPractice {
private String addressId;
private String name;
}
然后您需要使用以下数据模型将 Original
映射到 Restructured
对象:
public class Restructured {
private List<RestructuredSubRecords> subRecords;
}
public class RestructuredSubRecords {
private List<RestructuredSubRecordsPractice> practice;
private String Key;
}
public class RestructuredSubRecordsPractice {
private String addressId;
private String name;
}
映射它们非常简单,唯一的细节是您似乎只对 row
中的第一个 practice
感兴趣。
我在解析 json 并稍微更改响应结构时遇到问题。下面是我正在尝试解析的 json -
{
"subRecords": [
{
"row": [
{
"practice": [
{
"addressId": "123",
"name":"JON"
}
]
}
],
"Key": "123456-ABC"
}
]
}
我的预期输出是没有 row
-
{
"subRecords": [
{
"practice": [
{
"addressId": "123",
"name": "JON"
}
],
"Key": "123456-ABC"
}
]
}
下面是我的pojo -
@Getter
@Setter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Test{
private Map<String,Object>Key;
private List<Map<String,Object>>practice;
private List<Map<String, Object>> subRecords;
public List<Map<String, Object>> getSubRecords() {
return subRecords;
}
public void setSubRecords(List<Map<String, Object>> row) {
this.practice= (List<Map<String, Object>>) row.get(0).get("practice");
}
}
但我不断收到如下解析错误-
"errors": {
"status": "500",
"title": "Internal Server Error",
"detail": "N/A\n at [Source: (String)\
我不确定我是否正确访问 json 我将不胜感激任何帮助。
要将第一个 JSON 解析为 Java 对象,您需要以下数据模型:
public class Original {
private List<OriginalSubRecords> subRecords;
}
public class OriginalSubRecords {
private OriginalSubRecordsRow row;
private String Key;
}
public class OriginalSubRecordsRow {
private List<OriginalSubRecordsRowPractice> practice;
}
public class OriginalSubRecordsRowPractice {
private String addressId;
private String name;
}
然后您需要使用以下数据模型将 Original
映射到 Restructured
对象:
public class Restructured {
private List<RestructuredSubRecords> subRecords;
}
public class RestructuredSubRecords {
private List<RestructuredSubRecordsPractice> practice;
private String Key;
}
public class RestructuredSubRecordsPractice {
private String addressId;
private String name;
}
映射它们非常简单,唯一的细节是您似乎只对 row
中的第一个 practice
感兴趣。