如何为以下类型的 JSON 编写等效的 POJO:
[
{
"oneImageBlock": [{
"imagePath": "/abc",
"mobileImageOverride": {
"imagePath": "/abc"
},
"imageAltText": "image",
"imageCaptionText": "caption",
"captionTextReplacement": {
"type": "overlay"
},
"urlPath": "/url",
"displayURLInModal": true
}]
},
{
"fourImageBlock": [{
"imagePath": "/abc",
"mobileImageOverride": {
"imagePath": "/abc"
},
"imageAltText": "image",
"imageCaptionText": "caption",
"captionTextReplacement": {
"type": "overlay/Below Image"
},
"urlPath": "/l/123",
"displayURLInModal": true
},
{
"imagePath": "/abc",
"mobileImageOverride": {
"imagePath": "/abc"
},
"imageAltText": "image",
"imageCaptionText": "caption",
"captionTextReplacement": {
"type": "overlay/Below Image"
},
"urlPath": "/l/123",
"displayURLInModal": true
},
{
"imagePath": "/abc",
"mobileImageOverride": {
"imagePath": "/abc"
},
"imageAltText": "image",
"imageCaptionText": "caption",
"captionTextReplacement": {
"type": "overlay/Below Image"
},
"urlPath": "/l/123",
"displayURLInModal": true
},
{
"imagePath": "/abc",
"mobileImageOverride": {
"imagePath": "/abc"
},
"imageAltText": "image",
"imageCaptionText": "caption",
"captionTextReplacement": {
"type": "overlay/Below Image"
},
"urlPath": "/l/123",
"displayURLInModal": true
}
]
},
{
"fourImageBlock": [
]
},
{
"oneImageBlock": [
]
},
{
"oneTextBlock": [
"text": "test",
"textAlignment": "centre",
"mobileTextOverride": {
"text": "test"
},
"altText": "alt",
"urlPath": "/url"
]
},
{
"fourImageBlock": [
]
}
]
假设在 JSON 中可以有多个对象具有相同的键,就像这里的例子一样,它有 2 次出现
oneImageBlock
和 3 次出现 fourImageBlock
并且也没有固定的顺序,即在 JSON 中我们可能有前 5 个带有fourImageBlock
的物体,然后是 2 个带有oneImageBlock
的物体,然后是 2 个带有fourImageBlock
的物体。
反序列化数据的最简单方法是设置一些符合 JSON 数据的 POJO:
由于您的顶级对象可以包含键:
oneImageBlock
、fourImageBlock
或 oneTextBlock
,因此您需要一个对象来定义所有这三种可能性。它们在下面的Entry
类中定义。
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class BlockDeserializer {
protected static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws StreamReadException, DatabindException, IOException {
Entry[] entries = mapper.readValue(new File("src/main/resources/blocks.json"), Entry[].class);
System.out.println(entries);
}
private static class Entry {
public ImageBlock[] oneImageBlock;
public ImageBlock[] fourImageBlock;
public TextBlock[] oneTextBlock;
public TextBlock[] fourTextBlock;
}
private static class ImageBlock {
public String imagePath;
public MobileImageOverride mobileImageOverride;
public String imageAltText;
public String imageCaptionText;
public CaptionTextReplacement captionTextReplacement;
public String urlPath;
public boolean displayURLInModal;
}
private static class TextBlock {
public String text;
public String textAlignment;
public MobileTextOverride mobileTextOverride;
public String altText;
public String urlPath;
}
private static class CaptionTextReplacement {
public String type;
}
private static class MobileImageOverride {
public String imagePath;
}
private static class MobileTextOverride {
public String text;
}
}
此外,请确保您的
oneTextBlock
值是一个对象数组。看起来你的 JSON 无效。
[
//...
{
"oneTextBlock": [
{
"text": "test",
"textAlignment": "centre",
"mobileTextOverride": {
"text": "test"
},
"altText": "alt",
"urlPath": "/url"
}
]
},
// ...
]