本文共 5193 字,大约阅读时间需要 17 分钟。
表明Jackson支持反序列化“所有受支持类型的数组”,但是我无法确定确切的语法。
对于单个对象,我可以这样做:
// input{ "id" : "junk", "stuff" : "things"}//JavaMyClass instance = objectMapper.readValue(json, MyClass.class);
现在,对于数组,我想这样做:
//json input[{ "id" : "junk", "stuff" : "things"},{ "id" : "spam", "stuff" : "eggs"}]//JavaListentries = ?
有人知道是否有不可思议的命令吗? 如果没有,那么解决方案是什么?
从
ListmyObjects = Arrays.asList(mapper.readValue(json, MyClass[].class))
这个解决方案似乎对我来说是最好的
try { ObjectMapper mapper = new ObjectMapper(); JsonFactory f = new JsonFactory(); ListlstUser = null; JsonParser jp = f.createJsonParser(new File("C:\\maven\\user.json")); TypeReference
> tRef = new TypeReference
>() {}; lstUser = mapper.readValue(jp, tRef); for (User user : lstUser) { System.out.println(user.toString()); }} catch (JsonGenerationException e) { e.printStackTrace();} catch (JsonMappingException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}
对于通用实现:
public staticList parseJsonArray(String json, Class classOnWhichArrayIsDefined) throws IOException, ClassNotFoundException { ObjectMapper mapper = new ObjectMapper(); Class arrayClass = (Class ) Class.forName("[L" + classOnWhichArrayIsDefined.getName() + ";"); T[] objects = mapper.readValue(json, arrayClass); return Arrays.asList(objects);}
首先创建一个线程安全的ObjectReader实例。
ObjectMapper objectMapper = new ObjectMapper();ObjectReader objectReader = objectMapper.reader().forType(new TypeReference
>(){});
然后使用它:
Listresult = objectReader.readValue(inputStream);
这是一个可以转换json2object或Object2json的实用程序,无论您的pojo(实体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 staticT jsonStringToObject(String content, Class clazz) throws JsonParseException, JsonMappingException, IOException { T obj = null; ObjectMapper objMapper = new ObjectMapper(); obj = objMapper.readValue(content, clazz); return obj; } @SuppressWarnings("rawtypes") public static T jsonStringToObjectArray(String content) throws JsonParseException, JsonMappingException, IOException { T obj = null; ObjectMapper mapper = new ObjectMapper(); obj = mapper.readValue(content, new TypeReference () { }); return obj; } public static
T jsonStringToObjectArray(String content, Class 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; }
首先创建一个映射器:
import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3ObjectMapper mapper = new ObjectMapper();
作为数组:
MyClass[] myObjects = mapper.readValue(json, MyClass[].class);
作为列表:
ListmyObjects = mapper.readValue(jsonInput, new TypeReference
>(){});
指定列表类型的另一种方法:
ListmyObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
转载地址:http://nodnb.baihongyu.com/