android - Convert JSONArray to ArrayList of Custom Object Through GSON -


i getting response in form of json string.in response 1 field array of object or simple object eg.

type 1.

[{"0":1, "1":"name1", "id":1, "name":"name1"} , {"0":2, "1":"name2", "id":2, "name":"name2"}] 

type 2.

{"0":1, "1":"name1", "id":1, "name":"name1"} 

to handle case have created 2 model classes 1 array of object , 1 single object.

is there smart way handle this.

ok. so, want use gson.

first, go http://www.jsonschema2pojo.org/ , create relavent 1 pojo class.

below model class :

example.java

public class example {  @serializedname("0") @expose private integer _0; @serializedname("1") @expose private string _1; @serializedname("id") @expose private integer id; @serializedname("name") @expose private string name;  /** *  * @return * _0 */ public integer get0() { return _0; }  /** *  * @param _0 * 0 */ public void set0(integer _0) { this._0 = _0; }  /** *  * @return * _1 */ public string get1() { return _1; }  /** *  * @param _1 * 1 */ public void set1(string _1) { this._1 = _1; }  /** *  * @return * id */ public integer getid() { return id; }  /** *  * @param id * id */ public void setid(integer id) { this.id = id; }  /** *  * @return * name */ public string getname() { return name; }  /** *  * @param name * name */ public void setname(string name) { this.name = name; }  } 

now, not need create second model class if there jsonarray in response. can try out below ways arraylist<example>.

type collectiontype = new typetoken<arraylist<example>>() {}.gettype(); arraylist<example> triplist = triplistgson.fromjson(your json array string here, collectiontype); 

i hope out.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -