Java 找不到类 org.json.JSONObject 的序列化程序,也没有发现创建 BeanSerializer 的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26061781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:47:05  来源:igfitidea点击:

No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer

javajsonspringrestserialization

提问by abhishek ringsia

having JSON from web service, Json Array as a response

有来自 Web 服务的 JSON,Json Array 作为响应

   [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"
    }

After Getting Response in JsonArray Getting error while reading Json Object of Json Array :

在 JsonArray 中获得响应后在读取 Json Array 的 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) )

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

采纳答案by abhishek ringsia

have To Accept it first as a Json Array ,then while reading its Object have to use Object Mapper.readValue ,because Json Object Still in String .

必须首先将其作为 Json 数组接受,然后在读取其对象时必须使用 Object Mapper.readValue ,因为 Json 对象仍在 String 中。

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);
}