Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert JSONArray to List of Object using camel-jackson

Am having the String of json array as follow

{"Compemployes":[     {         "id":1001,         "name":"jhon"         },         {                 "id":1002,         "name":"jhon"         } ]} 

i want to convert this this jsonarray to List<Empolyee> . for this i had added the the maven dependency "camel-jackson" and also write the pojo class for employee . but when i try to run my below code

 ObjectMapper mapper = new ObjectMapper();  List<Employe> list = mapper.readValue(jsonString, TypeFactory.collectionType(List.class, Employe.class)); 

am getting the following exception.

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token  at [Source: java.io.StringReader@43caa144; line: 1, column: 1] 

can someone pls tell what am missing or doing anyting wrong

like image 487
Abhijeet Avatar asked Oct 09 '13 11:10

Abhijeet


People also ask

Can we convert JSONArray to JSONObject?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

How do I get JSONArray object?

JSONArray objects have a function getJSONObject(int index) , you can loop through all of the JSONObjects by writing a simple for-loop: JSONArray array; for(int n = 0; n < array. length(); n++) { JSONObject object = array. getJSONObject(n); // do some stuff.... }

Can we convert JSON array to list in Java?

Java For TestersWe can convert a JSON array to a list using the ObjectMapper class. It has a useful method readValue() which takes a JSON string and converts it to the object class specified in the second argument.

Is JSONArray a JSONObject?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.


2 Answers

The problem is not in your code but in your json:

{"Compemployes":[{"id":1001,"name":"jhon"}, {"id":1002,"name":"jhon"}]} 

this represents an object which contains a property Compemployes which is a list of Employee. In that case you should create that object like:

class EmployeList{     private List<Employe> compemployes;     (with getter an setter) } 

and to deserialize the json simply do:

EmployeList employeList = mapper.readValue(jsonString,EmployeList.class); 

If your json should directly represent a list of employees it should look like:

[{"id":1001,"name":"jhon"}, {"id":1002,"name":"jhon"}] 

Last remark:

List<Employee> list2 = mapper.readValue(jsonString,  TypeFactory.collectionType(List.class, Employee.class)); 

TypeFactory.collectionType is deprecated you should now use something like:

List<Employee> list = mapper.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class,      Employee.class)); 
like image 159
Frederic Close Avatar answered Oct 17 '22 00:10

Frederic Close


/*  It has been answered in http://stackoverflow.com/questions/15609306/convert-string-to-json-array/33292260#33292260  * put string into file jsonFileArr.json  * [{"username":"Hello","email":"[email protected]","credits"  * :"100","twitter_username":""},  * {"username":"Goodbye","email":"[email protected]"  * ,"credits":"0","twitter_username":""},  * {"username":"mlsilva","email":"[email protected]"  * ,"credits":"524","twitter_username":""},  * {"username":"fsouza","email":"[email protected]"  * ,"credits":"1052","twitter_username":""}]  */  public class TestaGsonLista {  public static void main(String[] args) { Gson gson = new Gson();  try {     BufferedReader br = new BufferedReader(new FileReader(             "C:\\Temp\\jsonFileArr.json"));     JsonArray jsonArray = new JsonParser().parse(br).getAsJsonArray();     for (int i = 0; i < jsonArray.size(); i++) {         JsonElement str = jsonArray.get(i);         Usuario obj = gson.fromJson(str, Usuario.class);         //use the add method from the list and returns it.         System.out.println(obj);         System.out.println(str);         System.out.println("-------");     }  } catch (IOException e) {     e.printStackTrace();  } } 
like image 26
Marlon Leite de Albuquerque Avatar answered Oct 17 '22 00:10

Marlon Leite de Albuquerque