복붙노트

[SPRING] org.json.JSONObject 클래스 용의 직렬자를 발견하지 못해, BeanSerializer를 작성하는 프로퍼티가 발견되지 않았다

SPRING

org.json.JSONObject 클래스 용의 직렬자를 발견하지 못해, BeanSerializer를 작성하는 프로퍼티가 발견되지 않았다

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

JsonArray에서 응답 받기 후 Json Array의 Json Object를 읽는 중 오류 발생 :

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

해결법

  1. ==============================

    1.먼저 Json 배열로 받아 들여야 만합니다. Json 객체가 여전히 String이기 때문에 Object를 읽는 동안 Object Mapper.readValue를 사용해야합니다.

    먼저 Json 배열로 받아 들여야 만합니다. Json 객체가 여전히 String이기 때문에 Object를 읽는 동안 Object Mapper.readValue를 사용해야합니다.

    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);
    }
    
  2. from https://stackoverflow.com/questions/26061781/no-serializer-found-for-class-org-json-jsonobject-and-no-properties-discovered-t by cc-by-sa and MIT license