更改json库以将json数组从com.fasterxml.jackson反序列化为org.codehaus.jackson?

时间:2016-10-31 09:19:49

标签: java json serialization jackson

我必须为“列表网络”解析以下json响应:

[
  {
    "Name": "bridge",
    "Id": "f2de39df4171b0dc801e8002d1d999b77256983dfc63041c0f34030aa3977566",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "Internal": false,
    "IPAM": {
      "Driver": "default",
      "Config": [
        {
          "Subnet": "172.17.0.0/16"
        }
      ]
    },
    "Containers": {
      "39b69226f9d79f5634485fb236a23b2fe4e96a0a94128390a7fbbcc167065867": {
        "EndpointID": "ed2419a97c1d9954d05b46e462e7002ea552f216e9b136b80a7db8d98b442eda",
        "MacAddress": "02:42:ac:11:00:02",
        "IPv4Address": "172.17.0.2/16",
        "IPv6Address": ""
      }
    },
    "Options": {
      "com.docker.network.bridge.default_bridge": "true",
      "com.docker.network.bridge.enable_icc": "true",
      "com.docker.network.bridge.enable_ip_masquerade": "true",
      "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
      "com.docker.network.bridge.name": "docker0",
      "com.docker.network.driver.mtu": "1500"
    }
  },
  {
    "Name": "none",
    "Id": "e086a3893b05ab69242d3c44e49483a3bbbd3a26b46baa8f61ab797c1088d794",
    "Scope": "local",
    "Driver": "null",
    "EnableIPv6": false,
    "Internal": false,
    "IPAM": {
      "Driver": "default",
      "Config": []
    },
    "Containers": {},
    "Options": {}
  },
  {
    "Name": "host",
    "Id": "13e871235c677f196c4e1ecebb9dc733b9b2d2ab589e30c539efeda84a24215e",
    "Scope": "local",
    "Driver": "host",
    "EnableIPv6": false,
    "Internal": false,
    "IPAM": {
      "Driver": "default",
      "Config": []
    },
    "Containers": {},
    "Options": {}
  }
]

现在,到目前为止,我一直在使用com.fasterxml.jackson将其转换为我想要的Network.java pojo类,它按预期工作。 这是我的旧代码:

public static List<Network> getNetworksFromResponse(String jsonInput, Network obj)
{
    ObjectMapper mapper = new ObjectMapper();
    try
    {
        return mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, Network.class));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return null;
}

其中jsonInput是上面的json响应字符串。这里的问题是没有根“网络”密钥来使用简单的List networkList pojo。所以我必须将整个json响应传递给方法以返回网络列表。

Network.java:

public class Network
{
    private String EnableIPv6;
    private String Driver;
    private String Name;
    private String Labels;
    private String Internal;
    private String Id;
    private Options Options;
    private String Scope;
    private IPAM IPAM;

    public String getEnableIPv6()
    {
        return EnableIPv6;
    }

    @JsonProperty("EnableIPv6")
    public void setEnableIPv6(String EnableIPv6)
    {
        this.EnableIPv6 = EnableIPv6;
    }

    public String getDriver()
    {
        return Driver;
    }

    @JsonProperty("Driver")
    public void setDriver(String Driver)
    {
        this.Driver = Driver;
    }

    public String getName()
    {
        return Name;
    }

    @JsonProperty("Name")
    public void setName(String Name)
    {
        this.Name = Name;
    }

    public String getInternal()
    {
        return Internal;
    }

    @JsonProperty("Internal")
    public void setInternal(String Internal)
    {
        this.Internal = Internal;
    }

    public String getId()
    {
        return Id;
    }

    @JsonProperty("Id")
    public void setId(String Id)
    {
        this.Id = Id;
    }

    public Options getOptions()
    {
        return Options;
    }

    @JsonProperty("Options")
    public void setOptions(Options Options)
    {
        this.Options = Options;
    }

    public IPAM getIPAM()
    {
        return IPAM;
    }

    @JsonProperty("IPAM")
    public void setIPAM(IPAM IPAM)
    {
        this.IPAM = IPAM;
    }

    public String getScope()
    {
        return Scope;
    }

    @JsonProperty("Scope")
    public void setScope(String Scope)
    {
        this.Scope = Scope;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [EnableIPv6 = " + EnableIPv6 + ", Driver = " + Driver + ", Name = " + Name + ", Labels = " + Labels
                + ", Internal = " + Internal + ", Id = " + Id + ", Options = " + Options + ", IPAM = " + IPAM + ", Scope = "
                + Scope + "]";
    }
}

现在有一些其他方法正在使用org.codehaus库,并且会发生jar冲突。所以我必须将我的com.fasterxml.jackson更改为com.codehaus.jackson。 我尝试将导入行更改为codehaus libs,它给了我这个错误:

The method getTypeFactory() is undefined for the type ObjectMapper

codehaus中此mapper.readValue(...)或getTypeFactory()方法的等效方法是什么? 或mapper.getTypeFactory().constructCollectionType(List.class, Network.class)的等效方法是什么 在org.codehaus.jackson?

1 个答案:

答案 0 :(得分:0)

com.fasterxml.杰克逊到org.codehaus.杰克逊&#34;的答案通常是&#34; Don&#t;#34;。这是因为前者意味着杰克逊2.x,维护版本具有更多功能;而后者杰克逊1.x在过去的4年中没有显着更新。

话虽如此,Jackson 1.x ObjectMapper应该有各种readValue()方法,并且可以访问TypeFactory。但可能需要通过config对象访问它;类似的东西:

mapper.getSerializationConfig().getTypeFactory();

(检查拼写;名称可能略有不同)