java - 反序列化 json 对象的嵌套数组返回 null

标签 java json jackson

我的 Json 看起来像(并且不可修改)

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

java POJO 代码如下所示

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));

}

在主线

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());
}

所以,问题是 SaleLine 没有按预期填充

最佳答案

您的 JSON 可能无效;例如您的问题的最新版本中缺少逗号。

如果问题是您的 JSON 在语法上无效,那么您需要在解析 JSON 之前对其进行破解,或者破解解析器以接受无效的 JSON。

另一方面,您的某些 JSON 记录可能缺少 SaleLineSaleLines 属性,或者改为 null的值之一。如果可能的话,添加一些空测试...并拒绝记录或处理丢失的数据。

关于java - 反序列化 json 对象的嵌套数组返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27770494/

相关文章:

java - 使用 ProcessBuilder 运行 shell 脚本

javascript - 将 JSON 数据从 php 传递到 html-data 属性,然后传递到 Javascript

ios - 在 TableView 中仅显示一次 json 数组中的重复元素 swift

java - jackson 抛出 nosuchmethod 错误

java - 并发跳过列表?也就是说,不是 ConcurrentSkipListSet

java - Java 中的 RSA 加密,PHP 中的解密

java - 将使用 ant 构建的 Java eclipse 项目迁移到 Gradle

java - 通过 webservice android 读取 JSON

java - Jackson 和 GSON 无法与 Google App Engine 一起使用

java - Jackson 可以仅使用注释反序列化为 Map<Long, String> 吗?