Mam a String
o następującej wartości:
[
{
"key1": "value11",
"key2": "value12"
},
{
"key1": "value21",
"key2": "value22"
}
]
I następująca klasa:
public class SomeClass {
private String key1;
private String key2;
/* ... getters and setters omitted ...*/
}
I chcę przeanalizować to do a List<SomeClass>
lub aSomeClass[]
Jaki jest najprostszy sposób na zrobienie tego za pomocą Jacksona ObjectMapper
?
Odpowiedzi:
W końcu to dostałem:
ObjectMapper objectMapper = new ObjectMapper(); TypeFactory typeFactory = objectMapper.getTypeFactory(); List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));
źródło
Druga odpowiedź jest poprawna, ale dla kompletności, oto inne sposoby:
List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { }); SomeClass[] array = mapper.readValue(jsonString, SomeClass[].class);
źródło
Kompletny przykład z tablicą. Zastąp „ constructArrayType () ” przez „ constructCollectionType () ” lub inny potrzebny typ.
import java.io.IOException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; public class Sorting { private String property; private String direction; public Sorting() { } public Sorting(String property, String direction) { this.property = property; this.direction = direction; } public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } public String getDirection() { return direction; } public void setDirection(String direction) { this.direction = direction; } public static void main(String[] args) throws JsonParseException, IOException { final String json = "[{\"property\":\"title1\", \"direction\":\"ASC\"}, {\"property\":\"title2\", \"direction\":\"DESC\"}]"; ObjectMapper mapper = new ObjectMapper(); Sorting[] sortings = mapper.readValue(json, TypeFactory.defaultInstance().constructArrayType(Sorting.class)); System.out.println(sortings); } }
źródło
Rozwiązałem ten problem, weryfikując plik json na JSONLint.com, a następnie używając Jacksona. Poniżej znajduje się kod tego samego.
Main Class:- String jsonStr = "[{\r\n" + " \"name\": \"John\",\r\n" + " \"city\": \"Berlin\",\r\n" + " \"cars\": [\r\n" + " \"FIAT\",\r\n" + " \"Toyata\"\r\n" + " ],\r\n" + " \"job\": \"Teacher\"\r\n" + " },\r\n" + " {\r\n" + " \"name\": \"Mark\",\r\n" + " \"city\": \"Oslo\",\r\n" + " \"cars\": [\r\n" + " \"VW\",\r\n" + " \"Toyata\"\r\n" + " ],\r\n" + " \"job\": \"Doctor\"\r\n" + " }\r\n" + "]"; ObjectMapper mapper = new ObjectMapper(); MyPojo jsonObj[] = mapper.readValue(jsonStr, MyPojo[].class); for (MyPojo itr : jsonObj) { System.out.println("Val of getName is: " + itr.getName()); System.out.println("Val of getCity is: " + itr.getCity()); System.out.println("Val of getJob is: " + itr.getJob()); System.out.println("Val of getCars is: " + itr.getCars() + "\n"); } POJO: public class MyPojo { private List<String> cars = new ArrayList<String>(); private String name; private String job; private String city; public List<String> getCars() { return cars; } public void setCars(List<String> cars) { this.cars = cars; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } RESULT:- Val of getName is: John Val of getCity is: Berlin Val of getJob is: Teacher Val of getCars is: [FIAT, Toyata] Val of getName is: Mark Val of getCity is: Oslo Val of getJob is: Doctor Val of getCars is: [VW, Toyata]
źródło