Comment utiliser Jackson pour désérialiser un tableau d’objects

La documentation de liaison de données Jackson indique que Jackson prend en charge la désérialisation des “tableaux de tous les types pris en charge”, mais je ne peux pas trouver la syntaxe exacte pour cela.

Pour un seul object, je ferais ceci:

//json input { "id" : "junk", "stuff" : "things" } //Java MyClass instance = objectMapper.readValue(json, MyClass.class); 

Maintenant, pour un tableau, je veux faire ceci:

 //json input [{ "id" : "junk", "stuff" : "things" }, { "id" : "spam", "stuff" : "eggs" }] //Java List ensortinges = ? 

Quelqu’un sait-il s’il existe une commande magique manquante? Si non, quelle est la solution?

Commencez par créer un mappeur:

 import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3 ObjectMapper mapper = new ObjectMapper(); 

Comme tableau:

 MyClass[] myObjects = mapper.readValue(json, MyClass[].class); 

Comme liste:

 List myObjects = mapper.readValue(jsonInput, new TypeReference>(){}); 

Une autre façon de spécifier le type de liste:

 List myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class)); 

De Eugene Tskhovrebov

 List myObjects = Arrays.asList(mapper.readValue(json, MyClass[].class)) 

Cette solution semble être la meilleure pour moi

Pour l’implémentation générique:

 public static  List parseJsonArray(Ssortingng 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); } 
 try { ObjectMapper mapper = new ObjectMapper(); JsonFactory f = new JsonFactory(); List lstUser = 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.toSsortingng()); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

Commencez par créer une instance de ObjectReader qui soit compatible avec les threads.

 ObjectMapper objectMapper = new ObjectMapper(); ObjectReader objectReader = objectMapper.reader().forType(new TypeReference>(){}); 

Alors utilisez-le:

 List result = objectReader.readValue(inputStream); 

voici un utilitaire qui est prêt à transformer json2object ou Object2json, quel que soit votre pojo (entité T)

 import java.io.IOException; import java.io.SsortingngWriter; 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(Ssortingng jsonInSsortingng) { try { final ObjectMapper mapper = new ObjectMapper(); mapper.readTree(jsonInSsortingng); return true; } catch (IOException e) { return false; } } public static Ssortingng serializeAsJsonSsortingng(Object object) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objMapper = new ObjectMapper(); objMapper.enable(SerializationFeature.INDENT_OUTPUT); objMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); SsortingngWriter sw = new SsortingngWriter(); objMapper.writeValue(sw, object); return sw.toSsortingng(); } public static Ssortingng serializeAsJsonSsortingng(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); } SsortingngWriter ssortingngWriter = new SsortingngWriter(); objMapper.writeValue(ssortingngWriter, object); return ssortingngWriter.toSsortingng(); } public static  T jsonSsortingngToObject(Ssortingng 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 jsonSsortingngToObjectArray(Ssortingng content) throws JsonParseException, JsonMappingException, IOException { T obj = null; ObjectMapper mapper = new ObjectMapper(); obj = mapper.readValue(content, new TypeReference() { }); return obj; } public static  T jsonSsortingngToObjectArray(Ssortingng 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; }