[英]Spring Boot parse nested json into separate objects
我有一个具有嵌套对象的 json object,如下所示:
{
"process": "create",
"processDescription": "some description",
"book": {
"id": "some unique id",
"bookName": "some name",
"bookISBN": "some number",
"bookDesc": "some description",
"author": {
"id": "a different ID",
"firstName": "some name",
"lastName": "some description"
},
"timestampUpdated": null,
"timestampCreated": 1672753599223,
"createdByUser": "Jane Doe",
"updatedByUser": null
},
"someField": "some data",
"anotherField": "more data"
}
在这个例子中,我只关心“书”object 和“作者”object。所以,我有 Java 这两个类。 我试过像这样读取 json 字符串:
JsonNode jsonObject = new ObjectMapper().readTree(jsonString);
JsonNode bookObj = jsonObject.get("book");
JsonNode authorObj = bookObj.get("author");
ObjectMapper objectMapper = new ObjectMapper();
AuthorClass = objectMapper.readValue(authorObj.asText(), AuthorClass .class);
但这会导致错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException:由于 [Source: (String)"" 处的输入结束,map 没有内容; 行:1,列:0]
要反序列化 json 文件的嵌套部分,您可以将 json 文件转换为JsonNode
object 并使用JsonNode#at
方法:
//jsonnode is equal to
//{"id":"a different ID","firstName":"some name","lastName":"some description"}
JsonNode authornode = mapper.readTree(json).at("/book/author");
然后,您可以使用treeToValue
方法将JsonNode
object 转换为AuthorClass
object:
@Data
public class AuthorClass {
private String id;
private String firstName;
private String lastName;
}
JsonNode authornode = mapper.readTree(json).at("/book/author");
AuthorClass author = mapper.treeToValue(authornode, AuthorClass.class);
//it prints
//AuthorClass(id=a different ID, firstName=some name, lastName=some description)
System.out.print(author);
您应该使用方法authorObj.toString()而不是方法 authorObj.asText()
工作示例:
public class Example {
public static void main(String[] args) throws JsonProcessingException {
String input = "{\n" +
" \"process\": \"create\",\n" +
" \"processDescription\": \"some description\",\n" +
" \"book\": {\n" +
" \"id\": \"some unique id\",\n" +
" \"bookName\": \"some name\",\n" +
" \"bookISBN\": \"some number\",\n" +
" \"bookDesc\": \"some description\", \n" +
" \"author\": {\n" +
" \"id\": \"a different ID\",\n" +
" \"firstName\": \"some name\",\n" +
" \"lastName\": \"some description\"\n" +
" },\n" +
" \"timestampUpdated\": null,\n" +
" \"timestampCreated\": 1672753599223,\n" +
" \"createdByUser\": \"Jane Doe\",\n" +
" \"updatedByUser\": null\n" +
" },\n" +
" \"someField\": \"some data\",\n" +
" \"anotherField\": \"more data\"\n" +
"}";
JsonNode jsonObject = new ObjectMapper().readTree(input);
JsonNode bookObj = jsonObject.get("book");
JsonNode authorObj = bookObj.get("author");
ObjectMapper objectMapper = new ObjectMapper();
AuthorClass authorClass = objectMapper.readValue(authorObj.toString(), AuthorClass.class);
System.out.println(authorClass.getFirstName());
}
}
class AuthorClass {
private String firstName;
private String lastName;
private String id;
public AuthorClass() {
}
public AuthorClass(String firstName, String lastName, String id) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.