How to use Jackson to deserialise an array of objects

前端 未结 8 1286
梦谈多话
梦谈多话 2020-11-21 22:58

The Jackson data binding documentation indicates that Jackson supports deserialising \"Arrays of all supported types\" but I can\'t figure out the exact syntax for this.

相关标签:
8条回答
  • 2020-11-21 23:22

    here is an utility which is up to transform json2object or Object2json, whatever your pojo (entity T)

    import java.io.IOException;
    import java.io.StringWriter;
    import java.util.List;
    
    import com.fasterxml.jackson.core.JsonGenerationException;
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    
    /**
     * 
     * @author TIAGO.MEDICI
     * 
     */
    public class JsonUtils {
    
        public static boolean isJSONValid(String jsonInString) {
            try {
                final ObjectMapper mapper = new ObjectMapper();
                mapper.readTree(jsonInString);
                return true;
            } catch (IOException e) {
                return false;
            }
        }
    
        public static String serializeAsJsonString(Object object) throws JsonGenerationException, JsonMappingException, IOException {
            ObjectMapper objMapper = new ObjectMapper();
            objMapper.enable(SerializationFeature.INDENT_OUTPUT);
            objMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
            StringWriter sw = new StringWriter();
            objMapper.writeValue(sw, object);
            return sw.toString();
        }
    
        public static String serializeAsJsonString(Object object, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
            ObjectMapper objMapper = new ObjectMapper();
            if (indent == true) {
                objMapper.enable(SerializationFeature.INDENT_OUTPUT);
                objMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
            }
    
            StringWriter stringWriter = new StringWriter();
            objMapper.writeValue(stringWriter, object);
            return stringWriter.toString();
        }
    
        public static <T> T jsonStringToObject(String content, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
            T obj = null;
            ObjectMapper objMapper = new ObjectMapper();
            obj = objMapper.readValue(content, clazz);
            return obj;
        }
    
        @SuppressWarnings("rawtypes")
        public static <T> T jsonStringToObjectArray(String content) throws JsonParseException, JsonMappingException, IOException {
            T obj = null;
            ObjectMapper mapper = new ObjectMapper();
            obj = mapper.readValue(content, new TypeReference<List>() {
            });
            return obj;
        }
    
        public static <T> T jsonStringToObjectArray(String content, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
            T obj = null;
            ObjectMapper mapper = new ObjectMapper();
            mapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
            obj = mapper.readValue(content, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
            return obj;
        }
    
    0 讨论(0)
  • 2020-11-21 23:25

    For Generic Implementation:

    public static <T> List<T> parseJsonArray(String json,
                                             Class<T> classOnWhichArrayIsDefined) 
                                             throws IOException, ClassNotFoundException {
       ObjectMapper mapper = new ObjectMapper();
       Class<T[]> arrayClass = (Class<T[]>) Class.forName("[L" + classOnWhichArrayIsDefined.getName() + ";");
       T[] objects = mapper.readValue(json, arrayClass);
       return Arrays.asList(objects);
    }
    
    0 讨论(0)
提交回复
热议问题