简体   繁体   English

从Json对象中提取数组

[英]Extract array from Json object

I want to extract an array from Json Object using GSON library. 我想使用GSON库从Json Object中提取数组。

I've already tried to create class and let Gson library do the work but in this particular case It doesn't work. 我已经尝试创建类并让Gson库完成工作,但在这种特殊情况下它不起作用。

Class for extracted data 提取数据的类

public class Images {
        private Image[] images;

        public class Image {
            private String url;

            public Image(String url) {
                this.url = url;
            }

            public String getUrl() {
                return url;
            }
        }

        public Images(Image[] images) {
            this.images = images;
        }

        public Image[] getImages() {
            return images;
        }
    }

Example data 示例数据

...
,
"images": [
   {
     "url": "https://a.allegroimg.com/original/030516/a867b9dd4021b15678fc03a3981b"
    },
    {
       "url": "https://a.allegroimg.com/original/03da09/19740f7147ad929609cc2bcc499e"
    },
    {
        "url": "https://a.allegroimg.com/original/03198b/c024c7e448cab876bb49ad055567"
    },
    {
        "url": "https://a.allegroimg.com/original/039fee/64bae4c64dac89e5b5b4d001c2ca"
     }
],
...

I'm getting error: "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 283 path $.items.promoted[0].images" 我收到错误:“com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第283行是BEGIN_ARRAY路径$ .items.promoted [0] .images”
I'm asking for help with extracting data from json object. 我正在寻求从json对象中提取数据的帮助。

Edit 编辑

I just needed to remove class 'Images' and save 'urls' from Json to Image[] 我只需要删除类'Images'并将'urls'从Json保存到Image []

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 283 path $.items.promoted[0].images 预计BEGIN_OBJECT但在第1行第283行路径为BEGIN_ARRAY $ .items.promoted [0] .images

The issue is due to the wrapper class Images . 问题是由于包装类Images From the error, I think your object is something like this: 从错误中,我认为你的对象是这样的:

class Promoted {
  private Images images;
  ..
}

class Images {
  private Image[] images;
}

In your current form, the json that you would be able to parse is: 在您当前的表单中,您将能够解析的json是:

...
,
"images": {
  "images": [
    {
      "url": "https://a.allegroimg.com/original/030516/a867b9dd4021b15678fc03a3981b"
    },
    {
      "url": "https://a.allegroimg.com/original/03da09/19740f7147ad929609cc2bcc499e"
    },
    {
      "url": "https://a.allegroimg.com/original/03198b/c024c7e448cab876bb49ad055567"
    },
    {
      "url": "https://a.allegroimg.com/original/039fee/64bae4c64dac89e5b5b4d001c2ca"
    }
  ]
}
...

Instead, you can directly embed the array Image[] in the parent object (without the wrapper class): 相反,您可以直接在父对象中嵌入数组Image[] (没有包装类):

class Promoted {
  private Image[] images;
  ..
}

You can use DSM library. 您可以使用DSM库。 By using it you can extract a part of JSON while you reading JSON data. 通过使用它,您可以在读取JSON数据时提取JSON的一部分。

First of all, you must define your mapping in yaml format. 首先,您必须以yaml格式定义映射。

Mapping Definition: 映射定义:

 result:
   type: array  # expect result as array
   path: /images
   fields:
     url:

Use DSM to read data. 使用DSM读取数据。

DSM dsm=new DSMBuilder(new File("path/to/config.yaml")).create();
Object object=dsm.toObject(new File("path/to/data.json");
System.out.println(object);

Here is the output: 这是输出:

[{url=https://a.allegroimg.com/original/030516/a867b9dd4021b15678fc03a3981b}, {url=https://a.allegroimg.com/original/03da09/19740f7147ad929609cc2bcc499e}, {url=https://a.allegroimg.com/original/03198b/c024c7e448cab876bb49ad055567}, {url=https://a.allegroimg.com/original/039fee/64bae4c64dac89e5b5b4d001c2ca}]

if you want to get all url field as a list you can charge your mapping definition as follows. 如果您想将所有url字段作为列表获取,则可以按如下方式为映射定义收费。

result:
   type: array
   path: /images/url

Output of this mapping is : 此映射的输出是:

[https://a.allegroimg.com/original/030516/a867b9dd4021b15678fc03a3981b, https://a.allegroimg.com/original/03da09/19740f7147ad929609cc2bcc499e, https://a.allegroimg.com/original/03198b/c024c7e448cab876bb49ad055567, https://a.allegroimg.com/original/039fee/64bae4c64dac89e5b5b4d001c2ca]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM