简体   繁体   English

如何解析 JSON 嵌套数组

[英]How can I parse JSON nested array

I have written this below code to read JSON and save in PostgreSQL, but it does not work when it is nested, only the last two records are added.我在下面写了这段代码来读取 JSON 并保存在 PostgreSQL 中,但是它在嵌套时不起作用,只添加了最后两条记录。 How can I loop nested arrray HD.如何循环嵌套数组 HD。 There 2 nested arrays HD in this case.在这种情况下,有 2 个嵌套的 arrays HD。 How can I make this work?我怎样才能使这项工作? should I have to loop HD array or am I doing something wrong here?我应该循环高清阵列还是我在这里做错了什么?

{
 "result": {
 "HD": [
  {
    "ADDR": "2218",
    "CAT": "s",
    "NAME": "Last"
  }
  ],
  "HD": [
  {
    "ADDR": "2219",
    "CAT": "w",
    "NAME": "Last"
  },
  "HD":
  {
    "ADDR": "2220",
    "CAT": "m",
    "NAME": "Last"
  }
  ]
}

My domain classes我的域类

@AllArgsConstructor
@Data
@Entity
@Table(name ="receive", schema = "public")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {

    @Id
    @JsonProperty("ADDR")
    @Column(name = "addr")
    private String ADDR;

    @JsonProperty("CAT")
    @Column(name = "cat")
    private String CAT;

    @JsonProperty("NAME")
    @Column(name = "name")
    private String NAME;
}
@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result {
    @JsonProperty("HD")
    List<Response> HD = new ArrayList<>();
    }

    @Getter
    public class AddressArray {
    Result result;
}

This is my main class;这是我的主class; I'm pretty sure I'm missing something here我很确定我在这里遗漏了一些东西

public class ReceiveApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReceiveApplication.class, args);
    }

    @Bean
    CommandLineRunner runner(ResponseService responseService) {
        return args -> {
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<AddressArray> typeReference1 = new TypeReference<AddressArray>() {
            };
            InputStream inputStream = TypeReference.class.getResourceAsStream("/json/response.json");
            try {
                AddressArray addressArray = mapper.readValue(inputStream, typeReference1);
                List<Response> responses = addressArray.getResult().getHD();
                responseService.save(responses);
                System.out.println(responses);
                System.out.println("Data saved in table");

            } catch (IOException e) {
                System.out.println("not saved " + e.getMessage());

            }

        };
    }
}

The problem is within the JSON, you have two HD entries there.问题出在 JSON 中,那里有两个 HD 条目。 First, this one:首先,这个:

"HD": [
  {
    "ADDR": "2218", ...
  }
]

and then a second one, at the same level:然后是第二个,在同一级别:

"HD": [
  {
    "ADDR": "2219", ...
  },
  "HD":
  {
    "ADDR": "2220", ...
  }
]

Imagine this is read as a key-value-map, with "HD" being the key.想象一下,这被读取为键值映射,其中“HD”是键。 During parsing, the first entry gets stored in the map, but it gets overwritten by the second entry, which has the same key.在解析期间,第一个条目存储在 map 中,但它被具有相同密钥的第二个条目覆盖。 So effectively, the first entry is lost and does not get stored.如此有效地,第一个条目会丢失并且不会被存储。
You did not mention where the input comes from, but if you are able to modify it, you should simply put all HD values in one array:你没有提到输入来自哪里,但如果你能够修改它,你应该简单地将所有 HD 值放在一个数组中:

"HD": [
  {
    "ADDR": "2218", ...
  },
  {
    "ADDR": "2219", ...
  },
  {
    "ADDR": "2220", ...
  }
]

Update:更新:
If changing the input is not an option, you could read the data in a more generic way as described here .如果更改输入不是一种选择,您可以按照此处所述以更通用的方式读取数据。
And to answer your other question, for reading attributes named HD,HD1,HD2 etc. you would simply put multiple members in your response class:要回答您的其他问题,对于读取名为 HD、HD1、HD2 等的属性,您只需将多个成员放入您的响应 class 中:

List<Response> HD = new ArrayList<>();
List<Response> HD1 = new ArrayList<>();
List<Response> HD2 = new ArrayList<>();

You get the picture;-)你得到了图片;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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