0
votes

I have following json response from webservice using retrofit 2.0 library. As retrofit uses Gson by default internally to map json into Java objects.

[
    {
        "album-96": {
            "id": 96,
            "m_id": 1,
            "user_id": null,
            "p_id": null,
            "category_id": 1,
            "sub_category_id": 12,
            "country_id": 226,
            "menu_id": null,
            "name": "Kate Gosselin Photos",
            "old_name": "Kate Gosselin Photos",
            "meta_description": "Katie Irene "Kate" Gosselin is an American television personality",
            "meta_keywords": "Television, Personality",
            "tags": "",
            "description": "<p>Katie Irene "Kate" Gosselin is an American television personality</p>",
            "total_file": 18,
            "view": 330,
            "url_str": "kate-gosselin-photos-96",
            "url": "/album/kate-gosselin-photos-96",
            "custom_url": "/album/96/kate-gosselin-photos",
            "old_url": "kate-gosselin-photos-96.html",
            "created_date": 1440843034,
            "update_date": 1440866729,
            "old": "no",
            "status": "publish",
            "c_id": 226,
            "c_name": "United States",
            "c_url": "united-states",
            "ca_id": 1,
            "ca_name": "Actress",
            "ca_url": "actress",
            "sca_id": 12,
            "sca_categories_id": 1,
            "sca_name": "Television",
            "sca_url": "television",
            "array": [
                {
                    "id": 5359,
                    "album_path": "/site-content/albums/Kate-Gosselin-Photos/",
                    "title": "Kate Gosselin",
                    "name": "5359.jpg",
                    "custom_url": "/photo/5359/kate-gosselin"
                },
                {
                    "id": 5358,
                    "album_path": "/site-content/albums/Kate-Gosselin-Photos/",
                    "title": "Kate Gosselin",
                    "name": "5358.jpg",
                    "custom_url": "/photo/5358/kate-gosselin"
                },
                {
                    "id": 5357,
                    "album_path": "/site-content/albums/Kate-Gosselin-Photos/",
                    "title": "Kate Gosselin",
                    "name": "5357.jpg",
                    "custom_url": "/photo/5357/kate-gosselin"
                },
                {
                    "id": 5356,
                    "album_path": "/site-content/albums/Kate-Gosselin-Photos/",
                    "title": "Kate gosselin",
                    "name": "5356.jpg",
                    "custom_url": "/photo/5356/kate-gosselin"
                },
                {
                    "id": 5355,
                    "album_path": "/site-content/albums/Kate-Gosselin-Photos/",
                    "title": "Kate Gosselin hair",
                    "name": "5355.jpg",
                    "custom_url": "/photo/5355/kate-gosselin-hair"
                },
                {
                    "id": 5354,
                    "album_path": "/site-content/albums/Kate-Gosselin-Photos/",
                    "title": "Kate gosselin",
                    "name": "5354.jpg",
                    "custom_url": "/photo/5354/kate-gosselin"
                }
            ]
        },
        "album-95": {
            "id": 95,
            "m_id": 1,
            "user_id": null,
            "p_id": null,
            "category_id": 4,
            "sub_category_id": 6,
            "country_id": 226,
            "menu_id": null,
            "name": "Christina Maria Aguilera",
            "old_name": "Christina Maria Aguilera",
            "meta_description": "Christina María Aguilera is an American singer, songwriter, and actress. Born in Staten Island, New York and raised in Rochester and Wexford, Pennsylvania",
            "meta_keywords": "Christina Maria Aguilera",
            "tags": "",
            "description": "<p>Christina María Aguilera is an American singer, songwriter, and actress. Born in Staten Island, New York and raised in Rochester and Wexford, Pennsylvania</p>",
            "total_file": 45,
            "view": 466,
            "url_str": "christina-maria-aguilera-95",
            "url": "/album/christina-maria-aguilera-95",
            "custom_url": "/album/95/christina-maria-aguilera",
            "old_url": "christina-maria-aguilera-95.html",
            "created_date": 1435346180,
            "update_date": 1435346329,
            "old": "no",
            "status": "publish",
            "c_id": 226,
            "c_name": "United States",
            "c_url": "united-states",
            "ca_id": 4,
            "ca_name": "Music",
            "ca_url": "music",
            "sca_id": 6,
            "sca_categories_id": 4,
            "sca_name": "Singer",
            "sca_url": "singer",
            "array": [
                {
                    "id": 5334,
                    "album_path": "/site-content/albums/Christina-Maria-Aguilera/",
                    "title": "Christina Aguilera",
                    "name": "5334.jpg",
                    "custom_url": "/photo/5334/christina-aguilera"
                },
                {
                    "id": 5333,
                    "album_path": "/site-content/albums/Christina-Maria-Aguilera/",
                    "title": "Christina Aguilera",
                    "name": "5333.jpg",
                    "custom_url": "/photo/5333/christina-aguilera"
                },
                {
                    "id": 5332,
                    "album_path": "/site-content/albums/Christina-Maria-Aguilera/",
                    "title": "Christina Aguilera",
                    "name": "5332.jpg",
                    "custom_url": "/photo/5332/christina-aguilera"
                },
                {
                    "id": 5331,
                    "album_path": "/site-content/albums/Christina-Maria-Aguilera/",
                    "title": "Christina Aguilera",
                    "name": "5331.jpg",
                    "custom_url": "/photo/5331/christina-aguilera"
                },
                {
                    "id": 5330,
                    "album_path": "/site-content/albums/Christina-Maria-Aguilera/",
                    "title": "Christina Aguilera",
                    "name": "5330.jpg",
                    "custom_url": "/photo/5330/christina-aguilera"
                },
                {
                    "id": 5329,
                    "album_path": "/site-content/albums/Christina-Maria-Aguilera/",
                    "title": "Christina Aguilera",
                    "name": "5329.jpg",
                    "custom_url": "/photo/5329/christina-aguilera"
                }
            ]
        }
    }
]

I have used http://www.jsonschema2pojo.org/ to generate model classes for my json. But it does not return me the list of items in Arraylist. Following is my code

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://girlvalue.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        GirlValueAPI girlValueAPI = retrofit.create(GirlValueAPI.class);
        Call<AlbumsList> call = girlValueAPI.loadAlbums("android");
        call.enqueue(this);

What should be model classes for above json response?

2
"As retrofit uses Gson by default internally to map json into Java objects" - Nope, that's no longer the case with Retrofit 2.0. You instructed it to use Gson. "But it does not return me the list of items in Arraylist" - Then what does it return? At a minimum you could provide the models you currently have. Also bear in mind that urls are resolved differently between Retrofit 1.x and 2.0. Just mentioning it in case your api base url has i.e. versioning information in its path.MH.

2 Answers

0
votes

Unfortunately, the structure of your JSON is pretty complicated. You can go two ways. First: create class with a field for each of this albums. Weak place: if more albums appear, the app will crash!

class BaseResponse {
@SerializedName("album-96")
Album album96;

@SerializedName("album-95")
Album album95;

private class Album{

    AlbumItem[] array;
    Integer id;
//all of the other fields for Album...
        }
}

The other alternative is two use custom deserializer

public AlbumDeserializer implements JsonDeserializer<Album>
{
    @Override
    public Album deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
//JSONElement here is your basic JSONArray retrieved from server.
        Album album = new Album();
        String data = json.getAsString();
        List<String> albums = Arrays.asList(data.split(" "));

        //check if string contains part like album-96 or album-95, or album+ number, using java regex. Work with that string.
    }
}
0
votes

You don't have a top level field in the JSON structure. It begins with an array instead. Try this:

Call<AlbumsList[]> call = girlValueAPI.loadAlbums("android");

And get the loadAlbums(..) method to return an array type.