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

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

Am having the String of json array as follow

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


        
5条回答
  •  感动是毒
    2020-12-23 10:27

    I also faced the similar problem with JSON output format. This code worked for me with the above JSON format.

    package com.test.ameba;
    
    import java.util.List;
    
    public class OutputRanges {
        public List OutputRanges;
        public String Message;
        public String Entity;
    
        /**
         * @return the outputRanges
         */
        public List getOutputRanges() {
            return OutputRanges;
        }
    
        /**
         * @param outputRanges the outputRanges to set
         */
        public void setOutputRanges(List outputRanges) {
            OutputRanges = outputRanges;
        }
    
        /**
         * @return the message
         */
        public String getMessage() {
            return Message;
        }
    
        /**
         * @param message the message to set
         */
        public void setMessage(String message) {
            Message = message;
        }
    
        /**
         * @return the entity
         */
        public String getEntity() {
            return Entity;
        }
    
        /**
         * @param entity the entity to set
         */
        public void setEntity(String entity) {
            Entity = entity;
        }
    }
    
    package com.test;
    
    
    public class Range {
        public String Name;
        /**
         * @return the name
         */
        public String getName() {
            return Name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            Name = name;
        }
    
        public Object[] Value;
        /**
         * @return the value
         */
        public Object[] getValue() {
            return Value;
        }
        /**
         * @param value the value to set
         */
        public void setValue(Object[] value) {
            Value = value;
        }
    
    }
    
    package com.test.ameba;
    
    import java.io.IOException;
    
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class JSONTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String jsonString ="{\"OutputRanges\":[{\"Name\":\"ABF_MEDICAL_RELATIVITY\",\"Value\":[[1.3628407124839714]]},{\"Name\":\" ABF_RX_RELATIVITY\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_Unique_ID_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_FIRST_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_AMEBA_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_Effective_Date_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_AMEBA_MODEL\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_UC_ER_COPAY_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_INN_OON_DED_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_COINSURANCE_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_PCP_SPEC_COPAY_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_INN_OON_OOP_MAX_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_IP_OP_COPAY_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_PHARMACY_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]},{\"Name\":\" ABF_PLAN_ADMIN_ERR\",\"Value\":[[\"CPD\",\"SL Limit\",\"Concat\",1,1.5,2,2.5,3]]}],\"Message\":\"\",\"Entity\":null}";
            ObjectMapper mapper = new ObjectMapper();
            OutputRanges OutputRanges=null;
            try {
                OutputRanges = mapper.readValue(jsonString, OutputRanges.class);
            } catch (JsonParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JsonMappingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("OutputRanges :: "+OutputRanges);;
            System.out.println("OutputRanges.getOutputRanges() :: "+OutputRanges.getOutputRanges());;
            for (Range r : OutputRanges.getOutputRanges()) {
                System.out.println(r.getName());
            }
        }
    
    }
    

提交回复
热议问题