错误:“JSONArray文本必须以'['at 1 [character 2 line 1]”开头,不应该适用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误:“JSONArray文本必须以'['at 1 [character 2 line 1]”开头,不应该适用相关的知识,希望对你有一定的参考价值。
我有一个通过JAXB初始化的ShipmentRequest类。
在我的主类中发送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>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="OrderItemId" type="{http://www.w3.org/2001/XMLSchema}long"/>
* <element name="ShipmentReference" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Transport" minOccurs="0">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="TransporterCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="TrackAndTrace" type="{https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd}TrackAndTrace" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* <element name="ShippingLabelCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </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>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="TransporterCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="TrackAndTrace" type="{https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd}TrackAndTrace" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </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>
错误是关于错误的对象类型。第一个字符应该是'[',但它不是 - 我猜它'{'。
这是因为你正在执行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类型的对象中。
以上是关于错误:“JSONArray文本必须以'['at 1 [character 2 line 1]”开头,不应该适用的主要内容,如果未能解决你的问题,请参考以下文章
Pig 安装错误:错误 pig.Main:错误 2998:未处理的内部错误