简体   繁体   中英

Deserialize nested array of json object returns null

My Json looks something like (and its unmodifiable)

{
    ....
    "Sale": [
        { "SaleLines": {
                    "SaleLine": [
                        {
                            "unitPrice": "190",
                            "unitQuantity": "1"
                        } 
                ], 
            "calcDiscount": "0",
            "calcSubtotal": "500"
        }
    } ]
}

The java POJO code looks like

public static class SaleLines {

    @JsonProperty("SaleLine")
    private ArrayList<SaleLine> saleLine;

    public ArrayList<SaleLine> getSaleLine() { return saleLine; }

    public void setSaleLine(ArrayList<SaleLine> saleLine) { this.saleLine = saleLine; }
}

public static class SaleLine {
    @JsonProperty("itemID")
    private String itemId;                  //line_item_nk
    @JsonProperty("unitPrice")
    private String unitPrice;
    ....
}

@JsonPropertyOrder({"total", "calcSubTotal", "calcDiscount"})
public static class Sale {

    private String saleTotal, calcSubtotal, calcDiscount; 
    private int salesValueWOVat;

    @JsonProperty("SaleLines")
    SaleLines saleLine;

    @JsonCreator
    public Sale (@JsonProperty("total")String saleTotal,
            @JsonProperty("calcSubtotal")String calcSubtotal,
            @JsonProperty("calcDiscount")String calcDiscount,
            @JsonProperty("SaleLines")SaleLines saleLine,
    ) {
        this.saleTotal = saleTotal;
        this.calcSubtotal = calcSubtotal;
        this.calcDiscount = calcDiscount;
        this.saleLine = saleLine;
        setSalesValueWOVat();
    }

    // getter and setters 

}

@SuppressWarnings({ "rawtypes" })
public static <E, T extends Collection> T readFromJsonAndFillType (
        String json, 
        Modules module,
        Class <T> collectionType,
        Class <E> elementType) 
        throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper objMapper = new ObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

    TypeFactory tf = objMapper.getTypeFactory();
    JsonNode node = objMapper.readTree(json).get(module.jsonFieldName); 
    return objMapper.readValue(node.toString(),
            tf.constructCollectionType(collectionType, elementType));

}

In main

ArrayList<Sale> saleList = readFromJsonAndFillType(
                saleJSON, 
                Modules.SALE, 
                ArrayList.class,
                Sale.class);

for (Sale sale: saleList) {
    System.out.println(sale.getSaleLines().getSaleLine().size()); //ERROR Null Pointer Exception
    System.out.println(sale.toString());
}

So, the problem is that the SaleLine does not get populated as expected

It is possible that your JSON is invalid; eg there are commas missing in the latest version in your Question.

If the problem is that your JSON is syntactically invalid, then you will either need to hack the JSON before you parse it or hack the parser to accept invalid JSON.

On the other hand, it is possible that some of your JSON records are missing the SaleLine or SaleLines attributes or have a null instead of one of the values. If that is possible, add some null tests ... and reject the record or cope with the missing data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM