gpt4 book ai didi

java - ModelMapper:如何将这个json映射到Java?

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

我正在尝试使用 ModelMapper 映射以下 json,如此处所述 http://modelmapper.org/user-manual/gson-integration/但我收到 NullPointerException 并且不知道出了什么问题。请问有什么建议吗?

{"a": "aaa", "b": [{"c": "ccc"}]}   

public class Foo {
private String a;
private ArrayList<Bar> b;
}



public class Bar {
private String c;
}

ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().addValueReader(new JsonElementValueReader());
JsonElement responseElement = new JsonParser().parse(json);
Foo foo = mapper.map(responseElement, Foo.class);

最佳答案

在查看您的意思和您在问题中的评论后,这很可能是他们实现中的一个错误。 ValueReader 声明的 javadoc

Returns all member names for the source object, else null if the source has no members.

但是,使用此方法的唯一代码 PropertyInfoSetResolver#resolveAccessors(...) 不会检查 null 是否存在。 JSON 中的成员名称仅对对象有意义,但是这里有一个 JSON 数组。这就是它失败的原因。

据我所知,代码不会检查 null 或没有成员的 source 类型。我认为这是一个错误。通过将任何字段(以及相应的 JSON)替换为数组类型,可以轻松地从示例示例中重现该错误。您可能需要联系开发人员或更改库。

关于java - ModelMapper:如何将这个json映射到Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951124/

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