gpt4 book ai didi

java - 使用 com.jackson 从 JSON 字符串获取 Java 对象的最佳方法?

转载 作者:行者123 更新时间:2023-11-30 02:02:43 26 4
gpt4 key购买 nike

具体来说,我正在处理一个 JSON 字符串,表示一个包含数字和其他数字数组的数组 - 这些字符串的形式为 String string = "[0, [1, 2], [3, 4] ]”。它表示 [channelID, [price, amount], [price, amount] ... [price, amount]] 形式的数据;重复数据的长度可以不同。

我的最终目标是将此字符串转换为 ChannelObject {channel: 0, data: [PriceObject {price: 1, amount: 2 }, PriceObject {price: 3, amount: 4 } 形式的对象] }

Jackson 的 native objectMapper 在尝试将未封装的 JSON 数组的值读取到类时抛出错误,我找到的唯一解决方案是为 ObjectMapper 解析器提供字符串 { "data": %s },替换原来的 JSON,并使用带有属性 @JsonProperty ArrayNode data 的类,但就性能而言,恐怕这是一种低效的方法。

为“裸”JSON 数组编写反序列化器是最明智的方法吗?我缺少更简单的解决方案吗?

非常感谢您的回复。

其他信息:

ObjectMapper 不起作用。

public static void main(String args[]) {
String input = "[17847,[5391.9,0,-1]";
ObjectMapper mapper = new ObjectMapper();
List<ChannelData> list = mapper.readValue(input, ChannelData[].class);
}

public class ChannelData {
private int channel;

private List<Data> data;

public ChannelData(int channel, List<Data> data) {
this.channel = channel;
this.data = data;
}

public int getChannel() {
return channel;
}

public void setChannel(int channel) {
this.channel = channel;
}

public List<Data> getData() {
return data;
}

public void setData(List<Data> data) {
this.data = data;
}
}

public class Data {
private BigDecimal price;

private int count;

private BigDecimal amount;

public Data() {
}

public Data(BigDecimal price, int count, BigDecimal amount) {
this.price = price;
this.count = count;
this.amount = amount;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public BigDecimal getAmount() {
return amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}
}

错误:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `co.myproject.Application$ChannelData` (no Creators, like default construct, exist): no int/Int-argument constructor/factory method to deserialize from Number value (17847)
at [Source: (String)"[17847,[5391.9,0,-1]]"; line: 1, column: 2] (through reference chain: java.lang.Object[][0])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromInt(ValueInstantiator.java:262)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromInt(StdValueInstantiator.java:356)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromNumber(BeanDeserializerBase.java:1324)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:173)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:195)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:21)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
at co.myproject.Application.run(Application.java:27)
at co.myproject.Application.main(Application.java:39)

Process finished with exit code 1

最佳答案

错误消息明确指出您的 ChannelData 中没有默认构造函数(即无参数构造函数)。类,在 ChannelData 中创建无参数构造函数类。

自从您声明 public ChannelData(int channel, List<Data> data) ChannelData有参构造函数,您有责任声明无参构造函数

public class ChannelData {
public ChannelData() { // no arg constructor }

关于java - 使用 com.jackson 从 JSON 字符串获取 Java 对象的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52304085/

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