java - Storing JSON Response in an array -


i trying store json array response arraylist. here json response looks like:

"identitylist":{     "identity":[         {             "firstname":"michael",             "lastname":"jameson",             "gender":"male",             "dateofbirth":"1961-05-18t00:00:00.000+0000",         },         {             "firstname":"kelly",             "lastname":"jameson",             "gender":"female",             "dateofbirth":"1951-04-01t00:00:43.000+0000",         }     ] } 

here's identity class:

public class identity {     /** first name. */     private string firstname;      /** middlename. */     private string middlename;      /** lastname. */     private string lastname;      /** dateofbirth. */     private localdate dateofbirth;      public identity(string firstname, string middlename, string lastname, localdate dateofbirth) {         super();         this.firstname = firstname;         this.middlename = middlename;         this.lastname = lastname;         this.dateofbirth = dateofbirth;     }      public string getfirstname() {         return firstname;     }      public string getmiddlename() {         return middlename;     }      public string getlastname() {         return lastname;     }      public localdate getdateofbirth() {         return dateofbirth;     } } 

based on i've found on other posts here wrote function parse it:

public static <t> t getresponseobjectasarray(string resourceresponse, string jsonobject, final class<t> responseclass) {     type listtype = new typetoken<list<responseclass>>(){}.gettype();     jsonarray jsonresponse = new jsonobject(resourceresponse).getjsonarray(jsonobject);     return gson.fromjson(jsonresponse, listtype); } 

and calling this:

getresponseobjectasarray(resourceresponse, "identitylist", identity.class) 

with resourceresponse being json response in string format. syntax error getresponseobjectasarray method says:

error:(39, 44) java: cannot find symbol   symbol:   class responseclass 

i'm trying parametrize list whatever class passed method because list of many other types , not identity. doing wrong here?

edit 1: tried solution of list<t> , got error now:

error:(41, 20) java: no suitable method found fromjson(org.json.jsonarray,java.lang.reflect.type)     method com.google.gson.gson.<t>fromjson(java.lang.string,java.lang.class<t>) not applicable       (cannot infer type-variable(s) t         (argument mismatch; org.json.jsonarray cannot converted java.lang.string))     method com.google.gson.gson.<t>fromjson(java.lang.string,java.lang.reflect.type) not applicable       (cannot infer type-variable(s) t         (argument mismatch; org.json.jsonarray cannot converted java.lang.string))     method com.google.gson.gson.<t>fromjson(java.io.reader,java.lang.class<t>) not applicable       (cannot infer type-variable(s) t         (argument mismatch; org.json.jsonarray cannot converted java.io.reader))     method com.google.gson.gson.<t>fromjson(java.io.reader,java.lang.reflect.type) not applicable       (cannot infer type-variable(s) t         (argument mismatch; org.json.jsonarray cannot converted java.io.reader))     method com.google.gson.gson.<t>fromjson(com.google.gson.stream.jsonreader,java.lang.reflect.type) not applicable       (cannot infer type-variable(s) t         (argument mismatch; org.json.jsonarray cannot converted com.google.gson.stream.jsonreader))     method com.google.gson.gson.<t>fromjson(com.google.gson.jsonelement,java.lang.class<t>) not applicable       (cannot infer type-variable(s) t         (argument mismatch; org.json.jsonarray cannot converted com.google.gson.jsonelement))     method com.google.gson.gson.<t>fromjson(com.google.gson.jsonelement,java.lang.reflect.type) not applicable       (cannot infer type-variable(s) t         (argument mismatch; org.json.jsonarray cannot converted com.google.gson.jsonelement)) 

edit 2: here's attempt @ parametrizing list object:

public static <t> list<t> getresponseobjectasarray(string resourceresponse, string jsonobject, class<t> responseclass) {     jsonarray jsonresponse = new jsonobject(resourceresponse).getjsonobject("identitylist").getjsonarray(jsonobject);     return gson.fromjson(jsonresponse.tostring(), list<responseclass>); } 

i hardcoded identitylist string later i'll make user input. i'm still unable parametrize list inside of fromjson call. i'm getting expression expected error.

thanks this post figured out how implement want. here's code now:

public static <t> list<t> getresponseobjectasarray(string resourceresponse, string jsonobject, class<t> responseclass) {     list<t> list = new arraylist<t>();     try {         list.add(responseclass.getconstructor().newinstance());     } catch(exception e) {         throw new runtimeexception(e);     }     jsonarray jsonresponse = new jsonobject(resourceresponse).getjsonobject("identitylist").getjsonarray(jsonobject);     return gson.fromjson(jsonresponse.tostring(), list.getclass()); } 

there's still stuff fix proper error handling , not hard coding getjson value.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -