如何拆分两个 json 字符串
How to split two json String
我有一个字符串,但 它包含两个对象,如下所示 {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"}
我得到了这个字符串作为响应,但我想一个一个地处理它,所以我应该如何将这个字符串分开,比如 {"firstName":"A","lastName":"Z"}
和 {"firstName":"B","lastName":"Y"}
Object mapper = new ObjectMapper();
List<YourClass> ls= ((ObjectMapper) mapper).readValue(
"[{"firstName":"A","lastName":"Z"},{"firstName":"B","lastName":"Y"}]",
new TypeReference<List<YourClass>>() {
});
//process list.
YourClass
映射 json 输入的 class。希望这就是您要找的。
编辑:
由于您的输入是以 {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"}
方式收到的,因此
String input = "{\"firstName\":\"A\",\"lastName\":\"Z\"}{\"firstName\":\"B\",\"lastName\":\"Y\"}";
String array[] = input.split("(?=\{)|(?<=\})");
System.out.println(array[0]);
System.out.println(array[1]);
输出:
{"firstName":"A","lastName":"Z"}
{"firstName":"B","lastName":"Y"}
我有一个字符串,但 它包含两个对象,如下所示 {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"}
我得到了这个字符串作为响应,但我想一个一个地处理它,所以我应该如何将这个字符串分开,比如 {"firstName":"A","lastName":"Z"}
和 {"firstName":"B","lastName":"Y"}
Object mapper = new ObjectMapper();
List<YourClass> ls= ((ObjectMapper) mapper).readValue(
"[{"firstName":"A","lastName":"Z"},{"firstName":"B","lastName":"Y"}]",
new TypeReference<List<YourClass>>() {
});
//process list.
YourClass
映射 json 输入的 class。希望这就是您要找的。
编辑:
由于您的输入是以 {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"}
方式收到的,因此
String input = "{\"firstName\":\"A\",\"lastName\":\"Z\"}{\"firstName\":\"B\",\"lastName\":\"Y\"}";
String array[] = input.split("(?=\{)|(?<=\})");
System.out.println(array[0]);
System.out.println(array[1]);
输出:
{"firstName":"A","lastName":"Z"}
{"firstName":"B","lastName":"Y"}