0

I want create a pojo from a json like this

{
        "1": [
            {
                "idmapel": 1,
                "label": "Fisika"
            },
            {
                "idmapel": 2,
                "label": "Kimia"
            },
            {
                "idmapel": 3,
                "label": "Biologi"
            },
            {
                "idmapel": 4,
                "label": "Matematika"
            },
        ],
  "2":[
    {
        "idmapel": 1,
        "label": "Fisika"
    }
  ]
}

when i generate from http://www.jsonschema2pojo.org/ it created a 1 and 2 class, but imagine if i have more than 2 keys. i want to be able to access element by something like this ObjectClass::getList(1) or ObjectClass::getList(2)

2
  • Could you elaborate? I'm imagining more than 2 keys, but nothing seems to be happening. You say it works - are you worried about scalability or something? If so, what are your concerns? Commented Sep 30, 2017 at 7:17
  • i am retrieving this from server with retrofit, the result may contain key 1,2,3, ..,n, depend on data, so what i want is i want to be able get collection by the key, if i wan to get collection with 1 key it should be accessible via ObjectClass::get(key) where key is 1, 2, or whatever the key on this json Commented Oct 1, 2017 at 3:53

1 Answer 1

1

You can use a Map to do this.

Map<String, List<Pojo>> map = deserialize(jsonSring);

where Pojo is the class which has fields idmapel and label, deserialize is a method which deserializes the json to object and jsonString is the json string value to deserialize. Then you can access lists with keys

List<Pojo> list1 = map.get("1");
List<Pojo> list2 = map.get("2");
Sign up to request clarification or add additional context in comments.

1 Comment

seems like best approach, will try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.