I want to extract an array from Json Object using GSON library.
I've already tried to create class and let Gson library do the work but in this particular case It doesn't work.
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"
I'm asking for help with extracting data from json object.
I just needed to remove class 'Images' and save 'urls' from Json to Image[]
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 283 path $.items.promoted[0].images
The issue is due to the wrapper class 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:
...
,
"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):
class Promoted {
private Image[] images;
..
}
You can use DSM library. By using it you can extract a part of JSON while you reading JSON data.
First of all, you must define your mapping in yaml format.
Mapping Definition:
result:
type: array # expect result as array
path: /images
fields:
url:
Use DSM to read data.
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.
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.