java - Get Inner Json String Data from Main Json String using Gson -
i using gson
parse data json string. working fine.
now, main json string
contains inner json string , want inner json string
's data. how ?
check json string
:
[{"question":"are inclined to:","qid":"2","options":[{"option":"argue or debate issues","optionid":"4"},{"option":"avoid arguments","optionid":"5"},{"option":"swutch topics","optionid":"6"}]}]
here, want value of "options".
sample code below :
primarytest[] testlist = gson.fromjson(result, primarytest[].class); // result json string. list<primarytest> lsttest = arrays.aslist(testlist); primarytest objtest = lsttest.get(0); string question = objtest.getquestion();
here, getting question value don't know how options value. if using same method giving error : invalid json data
. primarytest.java code :
public class primarytest { private string question; public primarytest(string question) { this.question = question; } public void setquestion(string question) { this.question = question; } public string getquestion() { return this.question; } }
write class include parameter json string contain can directly parse string class object.
primarytest
class should contain following members :
public class primarytest { private string question; private int qid; private list<option> options; //getter , setter methods here }
and option
class contain members shown below :
public class option{ private string option; private int optionid; // getter , setter methods }
now parse json string in primarytest
class , options
list getter method of class.
below test code :
primarytest[] response = gson.fromjson(json, primarytest[].class); list<primarytest> lsttest = arrays.aslist(response); primarytest objtest = lsttest.get(0); list<option> options = objtest.getoptions(); for(option option : options){ system.out.println(option.getoption()); }
Comments
Post a Comment