错误:“ JSONArray文本必须以'['开头1 [字符2第1行]“不适用

时间:2018-09-06 09:19:53

标签: java arrays rest jaxb

我有一个ShipmentRequest类,它是通过JAXB初始化的。

在要发送REST-API POST-Request的主类中,我用所需的输入填充此类的对象,并将其放入JSONArray

public JSONArray array = new JSONArray();
array.add(shipmentRequest);

最后我要开始POST:

HttpResponse<JsonNode> response = Unirest.post("https://someURL/services/rest/url")
.queryString("access_token","someTokenCode")
.body(array.get(0)).asJson();

我不理解该错误,因为当我使用myJSONArray.toJSONString()时,得到以下输出:

[{"Id":"123456","Reference":"Send","transport":{"transporterCode":"Deliverer","trackAndTrace":"someCode123456"}}]

我想念什么?我的第一个字符是'['?

编辑1,装运请求代码

//
// Diese Datei wurde mit der JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 generiert 
// Siehe <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Änderungen an dieser Datei gehen bei einer Neukompilierung des Quellschemas verloren. 
// Generiert: 2017.10.18 um 11:02:47 AM CEST 
//


package bol.shipment;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java-Klasse für anonymous complex type.
 * 
 * <p>Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="OrderItemId" type="{http://www.w3.org/2001/XMLSchema}long"/>
 *         &lt;element name="ShipmentReference" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="Transport" minOccurs="0">
 *           &lt;complexType>
 *             &lt;complexContent>
 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                 &lt;sequence>
 *                   &lt;element name="TransporterCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *                   &lt;element name="TrackAndTrace" type="{https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd}TrackAndTrace" minOccurs="0"/>
 *                 &lt;/sequence>
 *               &lt;/restriction>
 *             &lt;/complexContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *         &lt;element name="ShippingLabelCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "orderItemId",
    "shipmentReference",
    "transport",
})
@XmlRootElement(name = "ShipmentRequest")
public class ShipmentRequest {

    @XmlElement(name = "OrderItemId")
    protected String orderItemId;
    @XmlElement(name = "ShipmentReference", required = true)
    protected String shipmentReference;
    @XmlElement(name = "Transport")
    protected ShipmentRequest.Transport transport;

    /**
     * Ruft den Wert der orderItemId-Eigenschaft ab.
     * 
     */
    public String getOrderItemId() {
        return orderItemId;
    }

    /**
     * Legt den Wert der orderItemId-Eigenschaft fest.
     * 
     */
    public void setOrderItemId(String value) {
        this.orderItemId = value;
    }

    /**
     * Ruft den Wert der shipmentReference-Eigenschaft ab.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getShipmentReference() {
        return shipmentReference;
    }

    /**
     * Legt den Wert der shipmentReference-Eigenschaft fest.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setShipmentReference(String value) {
        this.shipmentReference = value;
    }

    /**
     * Ruft den Wert der transport-Eigenschaft ab.
     * 
     * @return
     *     possible object is
     *     {@link ShipmentRequest.Transport }
     *     
     */
    public ShipmentRequest.Transport getTransport() {
        return transport;
    }

    /**
     * Legt den Wert der transport-Eigenschaft fest.
     * 
     * @param value
     *     allowed object is
     *     {@link ShipmentRequest.Transport }
     *     
     */
    public void setTransport(ShipmentRequest.Transport value) {
        this.transport = value;
    }

    /**
     * <p>Java-Klasse für anonymous complex type.
     * 
     * <p>Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
     * 
     * <pre>
     * &lt;complexType>
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="TransporterCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
     *         &lt;element name="TrackAndTrace" type="{https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd}TrackAndTrace" minOccurs="0"/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "transporterCode",
        "trackAndTrace"
    })
    public static class Transport {

        @XmlElement(name = "TransporterCode")
        protected String transporterCode;
        @XmlElement(name = "TrackAndTrace")
        protected String trackAndTrace;

        /**
         * Ruft den Wert der transporterCode-Eigenschaft ab.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getTransporterCode() {
            return transporterCode;
        }

        /**
         * Legt den Wert der transporterCode-Eigenschaft fest.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setTransporterCode(String value) {
            this.transporterCode = value;
        }

        /**
         * Ruft den Wert der trackAndTrace-Eigenschaft ab.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getTrackAndTrace() {
            return trackAndTrace;
        }

        /**
         * Legt den Wert der trackAndTrace-Eigenschaft fest.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setTrackAndTrace(String value) {
            this.trackAndTrace = value;
        }

    }

}

编辑2: 我的请求应如下所示!

<ShipmentRequest xmlns="https://plazaapi.bol.com/services/xsd/v2.1/plazaapi.xsd">
 <OrderItemId>1234567890</OrderItemId>
 <ShipmentReference>A321SR</ShipmentReference>
 <Transport>
   <TransporterCode>TNT</TransporterCode>
   <TrackAndTrace>3SBOL0987654321</TrackAndTrace>
 </Transport>
</ShipmentRequest>

编辑3: 我尝试用这些填充JSONObject:

private JSONArray shipmentRequestArray = new JSONArray();
private JSONArray transportArray = new JSONArray();
private JSONObject requestObject = new JSONObject();

transportArray.add(new JSONObject().append("TransporterCode", transporter_Code));
transportArray.add(new JSONObject().append("TrackAndTrace", track_And_Trace));
shipmentRequestArray.add(new JSONObject().append("OrderItemId", orderItemIdTEST));
shipmentRequestArray.add(new JSONObject().append("ShipmentReference", shipment_Reference));
shipmentRequestArray.add(transportArray);

requestObject.put("ShipmentRequest", shipmentRequestArray);

当我尝试使用:

.body(requestObject).asJson();

我收到新错误: 没有找到类org.codehaus.jettison.json.JSONObject的序列化器,也没有发现用于创建BeanSerializer的属性

编辑4: 我的示例响应应如下所示:

Status: 201 created

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:ProcessStatus xmlns:ns1="https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd">
 <ns1:id>1</ns1:id>
 <ns1:sellerId>1084486</ns1:sellerId>
 <ns1:entityId>1234567890</ns1:entityId>
 <ns1:eventType>CONFIRM_SHIPMENT</ns1:eventType>
 <ns1:description>Confirm shipment for order item 1234567890.</ns1:description>
 <ns1:status>PENDING</ns1:status>
 <ns1:createTimestamp>2016-02-17T17:30:00.142+01:00</ns1:createTimestamp>
 <ns1:Links>
   <ns1:link ns1:method="GET" ns1:href="https://plazaapi.bol.com/services/rest/process-status/v2/1234567" ns1:rel="self"/>
 </ns1:Links>
</ns1:ProcessStatus>

1 个答案:

答案 0 :(得分:1)

错误与对象类型错误有关。第一个字符应该是'[',但不是-我猜是'{'。

这是因为您正在执行array.get(0),它会返回一个JSONObject,其中需要JSONArray。

UPD:

我使用您提供的其他信息进行了一些调试。 问题是Unirest.post返回了RequestBodyEntity类的对象。要将其转换为json,您需要首先通过getBody()获得答案。

那对我有用:

ShipmentRequest shipmentRequest = new ShipmentRequest();
shipmentRequest.setOrderItemId("1234567890");
shipmentRequest.setShipmentReference("A321SR");
ShipmentRequest.Transport transport = new ShipmentRequest.Transport();
transport.setTrackAndTrace("3SBOL0987654321");
transport.setTransporterCode("TNT");
shipmentRequest.setTransport(transport);

String jsonRequest = new Gson().toJson(shipmentRequest);
RequestBodyEntity result = Unirest.post("https://plazaapi.bol.com/services/rest/shipments/v2")
    .queryString(
       "access_token",
       "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjaWQiOjMzMTgsInNlcSI6MSwidHlwIjoiYXBpIiwidnNuIjoxLCJuYmYiOjE1MTcyOTc2NjF9.hVdtwBfJAVfRVeBdf1XDAxe7Qpigwa0wmCvnF_Bicc8")
    .body(jsonRequest);
System.out.println(new Gson().fromJson(result.getBody().toString(), ShipmentRequest.class));

您需要使用result.getBody()返回所需的对象。 之后,您可以

System.out.println(result.getBody().toString()) 

打印或

new Gson().fromJson(result.getBody().toString(), ShipmentRequest.class)

将其保存到ShipmentRequest类型的对象中。