猿问

无法将 JSON 响应从 com.sun.jersey.api.client

我正在尝试将 JSON 响应映射到 POJO,某些属性(属性以 @ 开头)无法映射,因此出现以下错误。


请找到 JSON 和 Class 文件,要对 POJO 进行哪些更改以映射下面提到的 JSON 的所有属性?


JSON


{

  "@customerId": "123456",

  "customerName": "Jobin",

  "orders": [

    {

      "orderId": "bvbundle002075",

      "address": {

        "@elid": "35475908"

      },


    }

  ]

}

客户.java


import java.util.List;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;


@XmlRootElement

@XmlType

public class CustomerData implements Serializable {


    private static final long serialVersionUID = 9163262900343406982L;


    private String customerId;

    private String customerName;

    private List<Order> orders;


    @XmlElement(name = "@customerId")

    public String getCustomerId() {

        return customerId;

    }


    public void setCustomerId(final String customerId) {

        this.customerId = customerId;

    }



    public String getCustomerName() {

        return customerId;

    }


    public void setCustomerName(final String customerName) {

        this.customerName = customerName;

    }


    public List<Order> getOrders() {

        return orders;

    }


    public void setExistingProducts(final List<Order> orders) {

        this.orders = orders;

    }

}

订单.java


class Order implements Serializable {


    private static final long serialVersionUID = 1L;

    private String orderId;

    private Address address;


    public String getOrderId() {

        return orderId;

    }


    public void setOrderId(final String orderId) {

        this.orderId = orderId;

    }


    public Address getAddress() {

        return address;

    }


    public void setAddress(final Address address) {

        this.address = address;

    }

}

地址.java


class Address implements Serializable {


    private static final long serialVersionUID = 1L;


    private String elid;


    @XmlElement(name = "@elid")

    public String getElid() {

        return elid;

    }


    public void setElid(String elid) {

        this.elid = elid;

    }

}


弑天下
浏览 151回答 1
1回答

慕森王

问题是我试图将 xml 属性映射到 xml 元素。如果您转换响应中提到的上述 JSON,它将产生如下所示的 XML..<?xml version="1.0" encoding="UTF-8"?><root customerId="123456">&nbsp; &nbsp;<customerName>Jobin</customerName>&nbsp; &nbsp;<orders>&nbsp; &nbsp; &nbsp; <element>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<address elid="35475908" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<orderId>bvbundle002075</orderId>&nbsp; &nbsp; &nbsp; </element>&nbsp; &nbsp;</orders></root>如您所见customerId,elid是 xml 属性而不是元素。所以我不得不使用下面的来修复它,@XmlAttribute(name = "customerNumber")&nbsp; //will map to @customerNumber in JSON@XmlAttribute(name = "elid")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //will map to @elid in JSON
随时随地看视频慕课网APP

相关分类

Java
我要回答