如何在 java 中将以下 JSON 转换为 POJO 以进行反序列化

问题描述 投票:0回答:1

如何为以下类型的 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
的物体。

java json jackson gson deserialization
1个回答
0
投票

反序列化数据的最简单方法是设置一些符合 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"
      }
    ]
  },
  // ...
]
© www.soinside.com 2019 - 2024. All rights reserved.