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

后端 未结 5 1770
Happy的楠姐
Happy的楠姐 2020-12-23 09:51

Am having the String of json array as follow

{\"Compemployes\":[
    {
        \"id\":1001,
        \"name\":\"jhon\"
        },
        {
                \"         


        
5条回答
  •  萌比男神i
    2020-12-23 10:32

    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 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 list2 = mapper.readValue(jsonString, 
    TypeFactory.collectionType(List.class, Employee.class));
    

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

    List list = mapper.readValue(jsonString,
    TypeFactory.defaultInstance().constructCollectionType(List.class,  
       Employee.class));
    

提交回复
热议问题