更新时间:2022-02-22 18:00:38
您需要启用 ACCEPT_SINGLE_VALUE_AS_ARRAY 功能.可能在 POJO
中你有一个 List
但是当 List
中只有一个元素时 JSON
有效载荷生成时没有数组括号.
You need to enable ACCEPT_SINGLE_VALUE_AS_ARRAY feature. Probably in POJO
you have a List
but when there is only one element in a List
JSON
payload is generated without array brackets.
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./src/main/resources/test.json");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Orgnization root = mapper.readValue(jsonFile, Orgnization.class);
System.out.println(root);
}
}