0

java object: MyObject has a list of AnotherObject1 and AnotherObject1 also have a list of AnotherObject2

class MyObject{

private String                  status;

private String                  message;

private List<AnotherObject1>            data;
public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<AnotherObject1> getData() {
        return data;
    }

    public void setData(List<AnotherObject1> data) {
        this.data = data;
        }
}

Class AnotherObject1{
private Integer                 group_id;

 private List<AnotherObject2>           anotherList;
public Integer getGroup_id() {
   return group_id;
}

public void setGroup_id(Integer group_id) {
   this.group_id = group_id;
}

public List<AnotherObject2> getAnotherList() {
  return smsList;
}

public void setAnotherList(List<AnotherObject2> anotherList) {
   this.anotherList = anotherList;
}

}

class AnotherObject2{
  private String                    customid;

    private String                  customid1   ;

    private Long                    mobile;

    private String                  status;

    private String                  country;

    public String getCustomid() {
        return customid;
    }

    public void setCustomid(String customid) {
        this.customid = customid;
    }

    public String getCustomid1() {
        return customid1;
    }

    public void setCustomid1(String customid1) {
        this.customid1 = customid1;
    }

    public Long getMobile() {
        return mobile;
    }

    public void setMobile(Long mobile) {
        this.mobile = mobile;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


}

JSON String: this is my json string by which i want to make an java object using object mapper

String response="{\"status\":\"OK\",\"data\":{\"group_id\":39545922,\"0\":{\"id\":\"39545922-1\",\"customid\":\"\",\"customid1\":\"\",\"customid2\":\"\",\"mobile\":\"910123456789\",\"status\":\"XYZ\",\"country\":\"IN\"}},\"message\":\"WE R Happy.\"}"

ObjectMapper code //convert string to response object

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.readValue(responseBody, MyObject.class);

exception: here is the exception

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: {"status":"OK","data":{"group_id":39545922,"0":{"id":"39545922-1","customid":"","customid1":"","customid2":"","mobile":"910123456789","status":"GOOD","country":"IN"}},"message":"We R happy."}; line: 1, column: 15] (through reference chain: MyObject["data"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:854)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:850)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:292)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:256)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714)
    at abc.disp(RestClientImpl.java:210)
    at abc.disp(RestClientImpl.java:105)
    at Application.<init>(Application.java:42)
    at Application.main(Application.java:45)

please guide me how to make it possible.

2
  • Well, I am not that familiar with JSON, but I could imagine that the 0 in the part "group_id" : ..., "0" : { ... } is a problem when trying to deserialize an instance of AnotherObject1. The instance field is not named 0, it is named smsList. Commented Aug 17, 2015 at 6:17
  • @Seelenvirtuose is correct. This is not a valid JSON String. Also, The JSON String does not contain any [] lists Commented Aug 17, 2015 at 6:26

3 Answers 3

2

Your code itself is not compailable ...

private List<AnotherObject1> data; // Your class member is list of AnotherObject1

and below it is used as List of SMSDTO in getter and setter

public List<SMSDTO> getData() {
    return data;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Problem is quite simple: you claim data should become Java List; and this requires that JSON input for it should be JSON Array. But what JSON instead has is a JSON Object.

So you either need to change POJO definition to expect something compatible with JSON Object (a POJO or java.util.Map); or JSON to contain an array for data.

Comments

0

First as Naveen Ramawat said your code is not compilable as it is.

  • In the class AnotherObject1 getSmsList should take AnotherObject2 and setSmsList should take AnotherObject2 also as parameter.
  • In the class MyObject setData and getData should use AnotherObject1 as parameters

Second your JSON string is not valid it should be sommething like that:

{"status":"OK","data":[{"group_id":39545922,"smsList":[{"customid":"39545922-1","customid1":"","mobile":913456789,"status":"XYZ","country":"XYZ"}]}]}

Here is the code that I used :

MyObject.java:

    import java.util.List;

    class MyObject {

        private String status;

        private String message;

        private List<AnotherObject1> data;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public List<AnotherObject1> getData() {
            return data;
        }

        public void setData(List<AnotherObject1> data) {
            this.data = data;
        }
    }

AnotherObject1.java :

    import java.util.List;

    public class AnotherObject1 {
        private Integer group_id;

        private List<AnotherObject2> smsList;

        public Integer getGroup_id() {
            return group_id;
        }

        public void setGroup_id(Integer group_id) {
            this.group_id = group_id;
        }

        public List<AnotherObject2> getSmsList() {
            return smsList;
        }

        public void setSmsList(List<AnotherObject2> smsList) {
            this.smsList = smsList;
        }
    }

AnotherObject2.java :

    public class AnotherObject2 {

        private String customid;

        private String customid1;

        private Long mobile;

        private String status;

        private String country;

        public String getCustomid() {
            return customid;
        }

        public void setCustomid(String customid) {
            this.customid = customid;
        }

        public String getCustomid1() {
            return customid1;
        }

        public void setCustomid1(String customid1) {
            this.customid1 = customid1;
        }

        public Long getMobile() {
            return mobile;
        }

        public void setMobile(Long mobile) {
            this.mobile = mobile;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

    }

To get the JSON string :

    import org.json.JSONObject;
    import org.json.XML;

    import com.google.gson.Gson;

    MyObject myObj = new MyObject();
    ArrayList<AnotherObject2> smsList = new ArrayList<AnotherObject2>();
    ArrayList<AnotherObject1> data = new ArrayList<AnotherObject1>();
    AnotherObject1 ao1 = new AnotherObject1();
    ao1.setGroup_id(39545922);
    ao1.setSmsList(smsList);
    AnotherObject2 sms = new AnotherObject2();
    sms.setCountry("XYZ");
    sms.setCustomid("39545922-1");
    sms.setCustomid1("");
    sms.setMobile((long) 913456789);
    sms.setStatus("XYZ");
    smsList.add(sms);
    ao1.setSmsList(smsList);
    data.add(ao1);
    myObj.setStatus("OK");
    myObj.setData(data);
    // Build a JSON string to display
    Gson gson = new Gson();
    String jsonString = gson.toJson(myObj);
    System.out.println(jsonString);
    // Get an object from a JSON string
    MyObject myObject2 = gson.fromJson(jsonString, MyObject.class);
    // Display the new object
    System.out.println(gson.toJson(myObject2));

2 Comments

yes i have to parse it ... i want to use object mapper to convert json string to java object
At the end of the code sample I've used you can find a conversion of a json string to a java object. To do so I used google gson : <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.