如何使用jackson将json转换为java中的POJO [英] How to convert json into POJO in java using jackson
问题描述
我正在使用spring 3.1.2,我需要将一个json对象解析为POJO。
这是我需要解析的json:
I'm using spring 3.1.2 and I need to parse a json object into POJO. This is the json that I need to parse:
{
"Person" : {
"id" : "2"
},
"Dog" : {
"dateOfBirth" : "2012-08-20 00:00:00",
"price" : "10.00"
}
}
我需要转换这个json对象(由两个对象组合而成)到一个POJO中,这里是:
I need to convert this json object (which is combined from two objects) into one POJO, here it is:
public class MyClass{
public MyClass(){}
public MyClass(String personsId, TimeStamp dogsDateOfBirth, BigDecimal dogsPrice){
.... // assign each parameter to the appropriate field
}
private String personsId;
private TimeStamp dogsDateOfBirth;
private BigDecimal dogsPrice;
//... Getters and Setters for each field
}
For那件事我用 ObjectMapper mapper = new ObjectMapper();
现在因为我有几个json对象,我的代码看起来像这样:
For that matter I used ObjectMapper mapper = new ObjectMapper();
Now since I have several json objects my code looks like this:
String json = ... ;// A json with several objects as above
JsonNode tree = mapper.readTree(json);
Iterator<JsonNode> iter = tree.path("data").getElements();
while (iter.hasNext()){
JsonNode node = iter.next();
MyClass myClass = mapper.readValue(node, MyClass.class);
... // do something with myClass object
}
当我运行这个 - 我得到以下异常:
When I run this - I get the following exception:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class ...MyClass]: can not instantiate from JSON object (need to add/enable type information?)
我试图创建一个简单的POJO - Person
:
I tried to create a simple POJO - Person
:
public class Person{
private String id;
public Person(){}
public Person(String id){
this.id = id;
}
... // Getter and Setter
}
并执行以下操作:
Person person = mapper.readValue(node.path("Person"), Person.class);
我得到这个(相同)例外:
I get this (same) exception:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class ...Person]: can not instantiate from JSON object (need to add/enable type information?)
我试着读一些关于类型信息 - 但无法理解它是如何帮助我的。
I tried to read some about the type information - but couldn't understand how it can help me here.
如何将此json转换为我的POJO?
How can I convert this json into my POJO?
谢谢。
推荐答案
我做的是这样的:
我创建了一个包含Person对象和Dog对象的新类,这些类需要是静态的(我发现它这里)。
以下是课程:
What I did was this: I created a new class that holds a Person object and Dog object, these classes needs to be static ( I found it here ). Here are the classes:
public static class MyNewClass{
private Person person;
private Dog dog;
... // two constructors and getters and setters
public static class Person{
private String id;
... // two constructors and getters and setters
}
public static class Dog{
private String dateOfBirth;
private String price;
... // two constructors and getters and setters
}
}
现在我的代码如下所示:
Now my code looks like this:
JsonNode tree = mapper.readTree(jsonString);
Iterator<JsonNode> iter = tree.path("data").getElements();
while (iter.hasNext()){
JsonNode node = iter.next();
Person person = mapper.readValue(node.path("Person"), Person.class);
Dog dog = mapper.readValue(node.path("Dog"), Dog.class);
MyNewClass myNewClass = new MyNewClass(person , dog);
... //Do something with it
}
我仍然想要在没有创建这两个对象(人和狗)的情况下这样做 - 现在已经足够了 - 但如果有人有想法 - 我想在这里!
I still want to do it without creating these two objects ( Person and Dog ) - That'a good enough for now - but if someone have an idea - I would like to here!
谢谢。
这篇关于如何使用jackson将json转换为java中的POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!