MCR
MCR

Reputation: 1643

JSON to GSON Objects

I am new to GSON and thus far have been successfully using it in my project. This JSON has been problematic for me...

 {
        "status": "6000",
        "action": "getProducts",
        "categories": {
            "type1": [
                {
                    "name": "name1",
                    "subtitle": "subtitle1"
                },
                {
                    "name": "name2",
                    "subtitle": "subtitle2"
                }
            ],
            "type2": [
                {
                    "name": "name3",
                    "subtitle": "name3"
                }
            ]
        }
    }

What's throwing me off is that I don't want to have to know the names "type1", "type2", etc. They will be used to dynamically populate a list view. What should my GSON objects looks like? or should my JSON be refactored? I have full control over the backend.

Upvotes: 0

Views: 102

Answers (1)

Blaz
Blaz

Reputation: 2075

You could do it like this

...
private Map<String, YourObject[]> categories;
...

or

...
private Map<String, List<YourObject>> categories;
...

and then you would call categories.keySet() to get set of type1, type2, ...

Upvotes: 3

Related Questions