Retrofit JSON

I have the following JSON channel:

{
  collection_name: "My First Collection",
  username: "Alias",
  collection: {
     1: {
        photo_id: 1,
        owner: "Some Owner",
        title: "Lightening McQueen",
        url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash1.jpg"
        },
     2: {
        photo_id: 2,
        owner: "Awesome Painter",
        title: "Orange Plane",
        url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash2.jpg"
        }
    }
}

What I'm trying to do is get the contents of the collection - photo_id, owner, title and URL. I have the following code, however, I am getting GSON JSON errors:

   @GET("/elliot/muzei/public/collection/{collection}")
    PhotosResponse getPhotos(@Path("collection") String collectionID);

    static class PhotosResponse {
        List<Photo> collection;
    }

    static class Photo {
        int photo_id;
        String title;
        String owner;
        String url;
    }
}

I thought my code was right to get the JSON channel, but I'm not sure. Any help was appreciated.

The error I am getting is:

Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 75

However, I'm struggling to figure out how to use the GSON library

+4
source share
1 answer

Your JSON is invalid.

GSON is waiting BEGIN_ARRAY "["after collection:, because your class PhotosResponsedefines the Photo array List<Photo>, but if found BEGIN_OBJECT "{", it must be

{
    "collection_name": "My First Collection",
    "username": "Alias",
    "collection": [
        {
            "photo_id": 1,
            "owner": "Some Owner",
            "title": "Lightening McQueen",
            "url": "http://hesp.suroot.com/elliot/muzei/public/images/randomhash1.jpg"
        },
        {
            "photo_id": 2,
            "owner": "Awesome Painter",
            "title": "Orange Plane",
            "url": "http://hesp.suroot.com/elliot/muzei/public/images/randomhash2.jpg"
        }
    ]
}

, JSON PHP json_encode() , JSON PHP , (PHP Array JSON, json_encode())

+5

Source: https://habr.com/ru/post/1527750/


All Articles