gpt4 book ai didi

java - 没有找到类 org.json.JSONObject 的序列化器,也没有发现创建 BeanSerializer 的属性

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:43 26 4
gpt4 key购买 nike

从 Web 服务获取 JSON,Json 数组作为响应

   [3]
0: {
id: 2
name: "a561137"
password: "test"
firstName: "abhishek"
lastName: "ringsia"
organization: "bbb"
}-
1: {
id: 3
name: "a561023"
password: "hello"
firstName: "hello"
lastName: "hello"
organization: "hello"
}-
2: {
id: 4
name: "a541234"
password: "hello"
firstName: "hello"
lastName: "hello"
organization: "hello"
}

在 JsonArray 中获取响应后读取 Json 数组的 Json 对象时出错:

List<User> list = new ArrayList<User>();
JSONArray jsonArr = new JSONArray(response);

for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
ObjectMapper mapper = new ObjectMapper();
User usr= mapper.convertValue(jsonObj, User.class);
list.add(usr);
}

No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )

最佳答案

必须首先接受它作为一个 Json 数组,然后在读取它的对象时必须使用 Object Mapper.readValue ,因为 Json 对象仍然在字符串中。

List<User> list = new ArrayList<User>();
JSONArray jsonArr = new JSONArray(response);

for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
ObjectMapper mapper = new ObjectMapper();
User usr = mapper.readValue(jsonObj.toString(), User.class);
list.add(usr);
}

关于java - 没有找到类 org.json.JSONObject 的序列化器,也没有发现创建 BeanSerializer 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26061781/

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