gpt4 book ai didi

java - 使用 Jackson 解析数组

转载 作者:行者123 更新时间:2023-12-02 11:18:36 27 4
gpt4 key购买 nike

我想使用 Jackson 解析以下 Json,但我完全不确定应该如何构建实体。

   [
CHAN_ID,
[
[
SYMBOL,
STATUS,
AMOUNT,
BASE_PRICE,
MARGIN_FUNDING,
MARGIN_FUNDING_TYPE,
PL,
PL_PERC,
PRICE_LIQ,
LEVERAGE,
...
],
...
]
]

这是我到目前为止所拥有的:

@Getter
@Setter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
public class Position {

@JsonProperty("SYMBOL")
private String symbol;
@JsonProperty("STATUS")
private String status;
@JsonProperty("AMOUNT")
private Decimal amount;
@JsonProperty("BASE_PRICE")
private Decimal basePrice;
@JsonProperty("MARGIN_FUNDING")
private Integer marginFunding;
@JsonProperty("MARGIN_FUNDING_TYPE")
private Decimal marginFundingType;
@JsonProperty("PL")
private Decimal profitLoss;
@JsonProperty("PL_PERC")
private Decimal profitLossPercentage;
@JsonProperty("PRICE_LIQ")
private Decimal liquidationPrice;
@JsonProperty("LEVERAGE")
private Decimal leverage;
}

我试图解析的这个东西,似乎有一个 Position 数组,但在它有 CHAN_ID 之前,我应该为此构造某种包装类吗?

@Getter
@Setter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
public class Positions {

@JsonProperty("CHAN_ID")
private String channelId;

@JsonProperty("positions")
private List<Position> positions;

}

您认为这是否正确?也不介意类顶部的那些注释,它只是 Lombok。目前,当我尝试解析时,此实现会出现以下错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of model.Positions out of START_ARRAY token at [Source: (String)"['ps', [ [ 'aa', 'bb', 123.45, 123.45, 567, 123.45, 123.45, 123.45, 123.45, 123.45]] ]"; line: 1, column: 1]

为了测试这一点,我只使用 ObjectMapper:

public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
try {
Positions positions = objectMapper.readValue("['ps', [ [ 'aa', 'bb', 123.45, 123.45, 567, 123.45, 123.45, 123.45, 123.45, 123.45]] ]", Positions.class);
} catch (IOException e) {
e.printStackTrace();
}

}

最佳答案

由于输入 JSON 的结构为嵌套数组,因此与 @JsonProperty 绑定(bind)没有多大意义,因为输入 JSON 中不存在这些属性键。仅当您想随后将反序列化数据转换为具有属性值对的 JSON 时,它们才有意义。

要正确读取此 JSON 结构,您必须实现自定义反序列化器,因为在这种情况下,属性无法自动与其值进行映射。反序列化器可以是 Jackson 的 JsonDeserializer 或 StdDeserializer 的实现。例如,以下实现有效:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class PositionsDeserializer extends StdDeserializer<Positions> {

public PositionsDeserializer() {
super(Positions.class);
}

@Override
public Positions deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Object[] positionsProps = p.readValueAs(Object[].class);
if (positionsProps != null && positionsProps.length > 0) {
String chanId = (String) positionsProps[0];
List<Position> positionsList =
((List<List<?>>) positionsProps[1]).stream()
.map(this::deserializePosition)
.collect(Collectors.toList());
Positions positions = new Positions();
positions.setChannelId(chanId);
positions.setPositions(positionsList);
return positions;
}
// decide whether you want to return null, throw an exception or other outcome: depends on the constraints of your data
return null;
}

private Position deserializePosition(List<?> props) {
if (props != null && !props.isEmpty()) {
final Position position = new Position();
position.setSymbol((String) props.get(0));
position.setStatus((String) props.get(1));
position.setAmount((Double) props.get(2));
position.setBasePrice((Double) props.get(3));
position.setMarginFunding((Integer) props.get(4));
position.setMarginFundingType((Double) props.get(5));
position.setProfitLoss((Double) props.get(6));
position.setProfitLossPercentage((Double) props.get(7));
position.setLiquidationPrice((Double) props.get(8));
position.setLeverage((Double) props.get(9));
return position;
}
// decide whether you want to return null, throw an exception or other outcome: depends on the constraints of your data
return null;
}

}

要使用此反序列化器,应在 ObjectMapper 中注册它:

private ObjectMapper initObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Positions.class, new PositionsDeserializer());
objectMapper.registerModule(module);
return objectMapper;
}

@Test
public void deserializePositions() throws IOException {
String json = "[ \"ps\", [ [ \"aa\", \"bb\", 123.45, 123.45, 567, 123.45, 123.45, 123.45, 123.45, 123.45 ] ] ]";

Positions positions = initObjectMapper().readValue(json, Positions.class);

System.out.println(positions);
}

建议

如果您可以选择/更改输入 JSON 的结构,我建议减少嵌套并引入更传统的 KV 映射。例如,要表示位置:

{
"CHAN_ID": "string",
"positions": [
// Use the arrays here: [ "aa", "bb", 123.45, 123.45, 567, 123.45, 123.45, 123.45, 123.45, 123.45 ],
// OR transform the array into KV mapping: { "SYMBOL": "string", ... }, ...
]
}

这种方法将允许:

  • 减少由与值的数组索引相关的魔数(Magic Number)引起的错误风险;

  • 简化反序列化:自动读取Positions,并仅在保留数组结构的情况下为Position提供自定义反序列化器。

另外,我不知道原始代码中的 Decimal 类型是什么意思,所以我将其替换为 Double

关于java - 使用 Jackson 解析数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085401/

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