Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

having JSON from web service, Json Array as a response

   [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 :

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

like image 679
abhishek ringsia Avatar asked Sep 26 '14 14:09

abhishek ringsia


1 Answers

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 .

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);
}
like image 99
abhishek ringsia Avatar answered Oct 22 '22 22:10

abhishek ringsia