我正在尝试解析datas.json中存在的JSON数据。我需要使用Beans / POJO类来解析排除数据.JSON数据如下:
$offset
我为ExclusionList创建了POJO类,但是无法在ECLIPSE IDE的控制台中获取和打印。Mypojo类:
$data
我的主要课程如下:
"ExclusionList" : {
"serviceLevel" : ["sl1","sl2","sl3"],
"item" : ["ABC","XYZ"] } }
在线程“ main” java.lang.NullPointerException
中获取作为 Exception的错误答案 0 :(得分:1)
You need to wrap your pojo class in another containing an ExclusionList property.
Try this. The examples below uses lombok for getters , setters and default constructor.
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
@Data
public class ExclusionListWrapper {
@JsonProperty("ExclusionList")
private ExclusionList exclusionList;
@Data
class ExclusionList {
List<String> serviceLevel;
List<String> item;
}
public static void main(String[] args) throws Exception {
String data = "{\"ExclusionList\" : {\"serviceLevel\" : [\"sl1\",\"sl2\",\"sl3\"], \"item\" : [\"ABC\",\"XYZ\"]}}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ExclusionListWrapper exclusionlistWrapper = mapper.readValue(data, ExclusionListWrapper.class);
List<String> excllist = exclusionlistWrapper.getExclusionList().getServiceLevel();
for (int i = 0; i < excllist.size(); i++) {
System.out.println(excllist.get(i));
}
}
}