如何使用 Jackson 将 JSON 字符串解析为数组

我有一个 String值如下:

[
{
"key1": "value11",
"key2": "value12"
},
{
"key1": "value21",
"key2": "value22"
}
]

还有下列课程:

public class SomeClass {
private String key1;
private String key2;
/* ... getters and setters omitted ...*/
}

我想把它解析成 List<SomeClass>或者 SomeClass[]

使用 杰克逊 ObjectMapper做到这一点最简单的方法是什么?

128283 次浏览

I finally got it:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));

The other answer is correct, but for completeness, here are other ways:

List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { });
SomeClass[] array = mapper.readValue(jsonString, SomeClass[].class);

The complete example with an array. Replace "constructArrayType()" by "constructCollectionType()" or any other type you need.

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);
}
}

I sorted this problem by verifying the json on JSONLint.com and then using Jackson. Below is the code for the same.

 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]