[英]Deserialize json using Java Jackson and based on the root element call different classes that match the Json Object within that root element
我是反序列化的新手,我正在嘗試為我的 class 反序列化復雜的 JSON。 我正在使用Jackson 2.12.1
。 我想知道如何在 map 以下 JSON 結構到我的 class 的多種情況下 Z1D78DC8ED14214E5AEZ24 到不同的根元素
我在類路徑的 Resources 文件夾中有一個 JSON 文件,它是這樣的:
{
"Employee":{
"firstName":"First Name",
"lastName":"Last Name",
"department":"Department"
},
"Student":{
"firstName":"Student First Name",
"lastName":"Student Last Name",
"studentID":"1234"
}
}
我有 2 個 class 分別用於Employee
和Car
,它擴展了Person
抽象 class 及其類似的東西:
@Getter
@Setter
public abstract class person{
private String firstName;
private String lastName;
}
@Getter
@Setter
@JsonRootName(value = "Employee")
public Employee extends person{
private String department;
}
@Getter
@Setter
@JsonRootName(value = "Student")
public Student extends person{
private String studentID;
}
public class MainClass{
public static void main(String []args){
String jsonFile = FileUtils.readFileToString(new File("src/main/resources/myFile.json"), "UTF-8");
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Employee eventInfo = objectMapper.readValue(jsonFile, Employee.class);
//This works for Employee but I want to make sure that it works for Student as well based on the ROOT ELEMENT value within the JSON
}
}
這適用於Employee
但我如何配置它以根據 JSON 中的不同 ROOT ELEMENT 值適用於所有類型? 我覺得我缺少一些基本的東西有人可以幫助我嗎?
PS:我正在使用來自 Project Lombok 的 @Getter 和 @Setter
我嘗試查看許多示例和 Jackson 的文檔。 我終於能夠讓它工作了。 這只是一個示例,可以用作映射 class 的基礎。 就我而言,我試圖根據映射到不同類的元素來查看存在的元素類型。 因此,這可能無法完全按照您的預期工作,但仍然可以作為參考。
我在這里發布相同的內容,以便將來對某人有所幫助:
如果以下是JSON文件內容:
[
{
"isA": "Type1",
"name": "Test",
"foo": "val1",
"foo": "val2",
"bar": "val3",
"foo": {
"myField": "Value1",
"myField": "value2"
}
},
{
"isA": "Type2",
"name": "Test1",
"foo": "val1",
"foo": "val2",
"bar": "val3",
"foo": {
"myField": "Value1",
"myField": "value2"
}
}
]
public void xmlConverter(InputStream jsonStream) throws IOException {
// Get the JSON Factory and parser Object
final JsonParser jsonParser = new JsonFactory().createParser(jsonStream);
final ObjectMapper objectMapper = new ObjectMapper();
jsonParser.setCodec(objectMapper);
// Check the first element is ARRAY if not then invalid JSON throw error
if (jsonParser.nextToken() != JsonToken.START_ARRAY) {
throw new IllegalStateException("Expected content to be an array");
}
jsonParser.nextToken();
// Loop until the end of the events file
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
// Get the node
final JsonNode jsonNode = jsonParser.readValueAsTree();
//Check if the JsonNode is valid if not then exit the process
if (jsonNode == null || jsonNode.isNull()) {
System.out.println("End Of File");
break;
}
// Get the eventType
final String eventType = jsonNode.get("isA").asText();
// Based on eventType call different type of class
switch (eventType) {
case "Type1":
final Type1 type1Info = objectMapper.treeToValue(jsonNode, Type1.class);
break;
case "Type2":
final Type2 type2Info = objectMapper.treeToValue(jsonNode, Type2.class);
break;
default:
System.out.println("JSON event does not match any of event : " + eventType);
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.