2

I'm parsing wikipedia API data, but don't get how to deal with changing serialized name on every new search request. It's always the first item of "pages" array (1156934). I am making request using Q204873 wiki data code to get page title, for example "Ammonium carbonate". But I don't know wikipedia pageID (1156934) before request.

wikipedia response

This response URL query


My gson model with marked problem (1156934 would fit here, but it changes as I make request with different wikidata Q code during new search query):

public class WikiPageList {

@SerializedName("WikiPage") //PROBLEM
private WikiPage wikiPage;

public void setWikiPage(WikiPage wikiPage){
    this.wikiPage = wikiPage;
}

public WikiPage getWikiPage(){
    return wikiPage;
}

@Override
public String toString(){
    return 
        "WikiPageList{" +
        "WikiPage = '" + wikiPage + '\'' +
        "}";
    }

}


How can I only deserialize first item of pages array? I can also supply multiple Q codes to receive multiple titles, but how to use same gson model for every array item?

Community
  • 1
  • 1
Neone
  • 482
  • 1
  • 9
  • 21
  • Possible duplicate of [what is the basic purpose of @SerializedName annotation in android using GSon](https://stackoverflow.com/questions/28957285/what-is-the-basic-purpose-of-serializedname-annotation-in-android-using-gson) – Crammeur Aug 04 '18 at 21:03
  • 1
    That doesn't answer my question. I know the purpose of @SerializedName and I use it to match title, pageid, etc in this example. But I can't extract this page first to be able to deserialize page futher as the pageID changes on every new request! – Neone Aug 04 '18 at 21:06
  • 1
    You need to create your own number deserialzer and register it with gson. Look at this example. let me know if you cannot understand. https://gist.github.com/peerapongsam/a7771e49562d74843c64f9cf53adbe52 – Abubakar Aug 04 '18 at 21:07

1 Answers1

5

You can use HashMap when you don't know the data key values in json

Use like below

@SerializedName("pages") public HashMap pages

You can also use custom deserializer to make it work

https://futurestud.io/tutorials/gson-advanced-custom-deserialization-basics

Viswanath Kumar Sandu
  • 2,230
  • 2
  • 17
  • 34