当前位置: 首页 > 知识库问答 >
问题:

找不到类org的序列化程序。json。JSONObject,未发现创建BeanSerializer的属性

上官波鸿
2023-03-14

有来自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);
}

没有找到JSONObject类org.json.序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationConfig. Feature.FAIL_ON_EMPTY_BEANS))

共有1个答案

夔学智
2023-03-14

必须首先接受它作为一个Json数组,然后在读取它的对象时必须使用对象Mapper.read值,因为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);
}
 类似资料: