0
{
  "batchcomplete": "",
  "warnings": {
    "main": {
      "*": "Unrecognized parameter: rvprop."
    },
    "extracts": {
      "*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
    }
  },
  "query": {
    "normalized": [
      {
        "from": "pune",
        "to": "Pune"
      }
    ],
    "pages": {
      "164634": {
        "pageid": 164634,
        "ns": 0,
        "title": "Pune",
        "extract": ""
                }
            }
      }
}

In the above json the numeric key inside "pages" object is dynamic. So how do i make a pojo for this json.

Please, please, please help me. I've searched a lot for this, but go nothing that works. Also i'm a beginner in retrofit, so please answer in detail. I've seen some answers which mention use of map for such cases (eg. Parse Dynamic Key Json String using Retrofit). But those answers are not elaborated properly. please help me understand it thoroughly.

  • Don't just Ask Question Directly.You have to try yourself ,And if you are not able to solve issue ,then ask with Demonstration. Look at this this may help you https://stackoverflow.com/questions/3527264/how-to-create-a-pojo – seon Jun 30 '17 at 10:30
  • seon, I know how to make pojo classes which is explained in that link. But the problem is with json having dynamic keys. Please help me if you know. – Sahil Patil Jun 30 '17 at 10:38
  • @SahilPatil Did you try with hashmap solution? – Krish Jun 30 '17 at 13:29

2 Answers2

3

You can use A hashmap for additional properties/ dynamic properties in your Pages class

private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
Sony
  • 7,136
  • 5
  • 45
  • 68
0
public class Query {
    private List<Address> normalized ;
    private Map<String, Pages> pages;
    //getter & Setter
    public Map<String, Pages>  getPages() {
        return pages;
    }
    public void setPages(Map<String, Pages>  pages) {
        this.pages = pages;
    }

    public class Pages {
        private String pageid;
        private int ns;
        private String title;
        private String extract;
        //define all getter and setter methods
    }

    public class Address{
    private String from;
    private String to;
    }
}

//add data in Pages class by using setter methods. Page pageObj = new Page(); pageObj.setPageid("164634"); pageObj.setns(0); ... Create local Map Map pagesObj = new HashMap<>(); pagesObj.put("164634", pageObj); use setPages(pagesObj); to set value

David Buck
  • 3,752
  • 35
  • 31
  • 35