Jackson Array to Pojo不适用于Android

时间:2019-08-09 11:16:44

标签: java android json jackson

我想将json数组转换为POJO,在JVM上运行时可以运行,但在Android上失败

这是我的pojo:

package com.binance.api.client.domain.market;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder()
@JsonIgnoreProperties(ignoreUnknown = true)

public class Lilin {
    public Long openTime;

    public String open;

    public String high;

    public String low;

    public String close;

    public String volume;

    public Long closeTime;

    public String quoteAssetVolume;

    public Long numberOfTrades;

    public String takerBuyBaseAssetVolume;

    public String takerBuyQuoteAssetVolume;
}

然后手动进行测试:

public void testCandlestickDeserializer() {
        final String candlestickJson = "[\n" +
                "    1499040000000,\n" +
                "        \"0.01634790\",\n" +
                "        \"0.80000000\",\n" +
                "        \"0.01575800\",\n" +
                "        \"0.01577100\",\n" +
                "        \"148976.11427815\",\n" +
                "        1499644799999,\n" +
                "        \"2434.19055334\",\n" +
                "        308,\n" +
                "        \"1756.87402397\",\n" +
                "        \"28.46694368\",\n" +
                "        \"17928899.62484339\"\n" +
                "        ]";
        ObjectMapper mapper = new ObjectMapper();
        try {
            Lilin candlestick = mapper.readValue(candlestickJson, Lilin.class);
            System.out.println(candlestick);
        } catch (IOException e) {
            System.err.println(e);
        }
    }

在JVM上尝试时没有错误,但是在Android上运行时会引发此错误:

Cannot deserialize value of type `java.lang.Long` from String "0.01634790": not a valid Long value

似乎@JsonPropertyOrder()注释在Android上无法正常工作

1 个答案:

答案 0 :(得分:1)

您可能错过了定义属性顺序的方法,例如在文档中:

示例:

// ensure that "id" and "name" are output before other properties
@JsonPropertyOrder({ "id", "name" })
// order any properties that don't have explicit setting using alphabetic order

@JsonPropertyOrder(alphabetic=true)
//This annotation may or may not have effect on deserialization: for basic JSON handling there is no effect, but for other supported data types (or structural conventions) there may be.

来源:https://fasterxml.github.io/jackson-annotations/javadoc/2.2.0/com/fasterxml/jackson/annotation/JsonPropertyOrder.html