如何使用Jackson ObjectMapper转换为Pojo以获取多个数据 [英] How to use Jackson ObjectMapper to convert to Pojo for multiple data

查看:327
本文介绍了如何使用Jackson ObjectMapper转换为Pojo以获取多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下字符串/ JSONObject转换为POJO,

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}"}

如何使用Jackson ObjectMapper转换为Pojo。
2个Pojo类如下。

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

上面字符串中的用户部分应映射到java文件 - User.java

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;
    }   
}

上面字符串中的List部分应映射到java文件 - TestCase.java

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;
    }       
}


推荐答案

自您的JSON对象不包含任何类型信息,最好的方法是使用杰克逊的自定义反序列化程序,至少对于外层阶级。或者,您可以尝试使用 Jackson注释来注释您的POJO类,以及希望正确的事情发生。

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.

无论如何,你必须通过调用 ObjectMapper.readValue() 具有正确类类型参数的方法,以便杰克逊知道正在反序列化的是什么。

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.

这篇关于如何使用Jackson ObjectMapper转换为Pojo以获取多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆