gpt4 book ai didi

java - Jackson 数组到 Pojo 只能在 Android 上运行

转载 作者:行者123 更新时间:2023-12-02 09:36:55 29 4
gpt4 key购买 nike

我想将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 上无法正常工作

最佳答案

您可能错过了定义属性顺序,例如从文档中:

示例:

// 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

关于java - Jackson 数组到 Pojo 只能在 Android 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57428923/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com