使用Jackson嵌套数组反序列化JSON

时间:2013-09-03 13:12:14

标签: java json jackson deserialization

我正在尝试将JSON数据反序列化为自定义类。我无法正确地反序列化嵌套数组,抛出异常。我查看了各种杰克逊注释,但我没有看到任何可能有用的东西。

JSON:

{
"purchaseOrderLines": [
    {
        "jsId": 1,
        "part": 26,
        "partNumber": "MS-12345",
        "quantityOrdered": "1000",
        "price": ".50",
        "unit": "EACH",
        "dueDate": "2013-12-31",
        "requirements": [
            [
                {
                    "requirement": "degrease",
                    "price": "25",
                    "jsId": 0
                }
            ]
        ],
        "class": "purchaseOrder.PurchaseOrderLine"
    }
],
"vendor": "39",
"shipper": "53",
"shipMethod": "3",
"vendorSalesman": "41"
}

PurchaseOrder.java

package purchaseOrder;

import java.util.ArrayList;
import java.util.List;
import models.addressBook.*;
import play.db.jpa.Model;

public class PurchaseOrder extends Model{
    public Vendor vendor;
    public Contact vendorSalesman;
    public Shipper shipper;
    public ShipMethod shipMethod;
    public String notes;
    public List<PurchaseOrderLine> purchaseOrderLines= new ArrayList<PurchaseOrderLine>();
    public List<Requirement> globalRequirements= new ArrayList<Requirement>();
}

PurchaseOrderLine.java

package purchaseOrder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;   
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import models.inventory.Part;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import purchaseOrder.util.UnitType;

@JsonIgnoreProperties(ignoreUnknown = true)
public class PurchaseOrderLine {

    public Part part;
    public Long quantityOrdered;
    public BigDecimal price;
    public UnitType unit;
    public DateTime dueDate;
    public List<Requirement> requirements= new ArrayList<Requirement>();

    public void setDueDate(String dueDate)
    {
        this.dueDate = ISODateTimeFormat.date().parseDateTime(dueDate);
    }
}

Requirement.java

package purchaseOrder;

import com.fasterxml.jackson.annotation.*;
import play.db.jpa.Model;

@JsonIgnoreProperties(ignoreUnknown = true)

public class Requirement extends Model
{
     public String requirement;
     public String price;
}

例外:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of purchaseOrder.Requirement out of START_ARRAY token
 at [Source: java.io.StringReader@7fc99326; line: 1, column: 160] (through reference chain: PurchaseOrder["purchaseOrderLines"]->PurchaseOrderLine["requirements"])
        at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:163)
        at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:588)
        at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:582)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:512)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:129)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
        at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:336)
        at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:87)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:290)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
        at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:336)
        at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:87)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:290)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2580)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1810)
        at controllers.PurchaseOrderController.submitQuote(PurchaseOrderController.java:24)
        at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
        ... 1 more

我知道反序列化需求类很好,因为我可以毫无问题地反序列化globalRequirements。如果我省略purchaseOrderLine的要求,我也可以反序列化。感谢您提供的任何帮助/指示

0 个答案:

没有答案