У меня есть String
следующее значение:
[
{
"key1": "value11",
"key2": "value12"
},
{
"key1": "value21",
"key2": "value22"
}
]
И следующий класс:
public class SomeClass {
private String key1;
private String key2;
/* ... getters and setters omitted ...*/
}
И я хочу разобрать его на a List<SomeClass>
или aSomeClass[]
Какой самый простой способ сделать это с помощью Джексона ObjectMapper
?
Ответы:
Я наконец понял:
ObjectMapper objectMapper = new ObjectMapper(); TypeFactory typeFactory = objectMapper.getTypeFactory(); List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));
источник
Другой ответ правильный, но для полноты, вот другие способы:
List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { }); SomeClass[] array = mapper.readValue(jsonString, SomeClass[].class);
источник
Полный пример с массивом. Замените « constructArrayType () » на « constructCollectionType () » или любой другой тип, который вам нужен.
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); } }
источник
Я решил эту проблему, проверив json на JSONLint.com, а затем используя Jackson. Ниже приведен код того же.
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]
источник