How to use Jackson ObjectMapper to convert to Pojo for multiple data

大兔子大兔子 提交于 2019-12-11 06:45:53

问题


I would like to convert the following string/ JSONObject to POJO,

{"list":["\r\n{\r\n\"id\":\"1\",\r\n\"protocol\":\"udp\",\r\n\"srcPorts= \":\"3000-4000    \",\r\n\"destPorts\":\"1790-2000\"\r\n}","\r\n{\r\n\"id\":\"2\",\r\n    \"protocol\":\"tcp\",\r\n\"srcPorts\":\"3000-4000\",\r\n\"destPorts\":\"1790-2000    \"\r\n}"],"user":"\r\n{\r\n\"name\":\"John\",\r\n\"address\":\"x.x.x.x\",\r\n\"email     \":\"john@p.com\"\r\n}"}

How do I convert to Pojo using Jackson ObjectMapper. The 2 Pojo classes are as follows.

The user part in the string above should map to the java file - User.java

public class User 
{
    private String name;

    private String address;

    private String email;

    public String getName()
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public String getAddress() 
    {
        return address;
    }

    public void setaddress(String Address) 
    {
        this.address = address;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }   
}

The List part in the string above should map to the java file - TestCase.java

public class TestCase 
{
    private String id;
    private String protocol;
    private String srcPorts;
    private String destPorts;

    public String getProtocol()
    {
        return protocol;
    }

    public void setProtocol(String protocol) 
    {
        this.protocol = protocol;
    }

    public String getSrcPorts() 
    {
        return srcPorts;
    }

    public void setSrcPorts(String srcPorts) 
    {
        this.srcPorts = srcPorts;
    }

    public String getDestPorts() 
    {
        return destPorts;
    }

    public void setDestPorts(String destPorts) 
    {
        this.destPorts = destPorts;
    }

    public String getID() 
    {
        return id;
    }

    public void setID(String id) 
    {
        this.id = id;
    }       
}

回答1:


Following code should help.

class ParseJson{

private User user;
private TestCase testCase;

//getter and setter methods
}
//and then call objectMapper - 
String jsonString = "";//Json input
ObjectMapper mapper = new ObjectMapper();
ParseJson parsedJson = mapper.readValue(jsonString, ParseJson.class);

User user = parsedJson.getUser();
TestCase testCase = parsedJson.getTestCase();




回答2:


Since your JSON object does not contain any type information, the best approach would be to use a custom deserializer class for Jackson, at least for the outer class. Alternatively, you can try annotating your POJO classes with Jackson annotations, and hope that the Right Thing happens.

In any case, you will have to make Jackson aware of your context by calling one of the ObjectMapper.readValue() methods with the proper class type argument, so that Jackson will know what it is that is being deserialized.



来源:https://stackoverflow.com/questions/22680630/how-to-use-jackson-objectmapper-to-convert-to-pojo-for-multiple-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!