How to extract data from a json object in android -


in application receiving json object

{"secquelist":{"1":"which favorite book?","2":"who childhood hero?","3":"what pet's name?","4":"what make first car or bike?","5":"what favorite color?","6":"which favorite sports team?","7":"what name of school?","8":"what mother's maiden name?","9":"which birthplace?","10":"which favourite sport?","11":"which favourite place of visit?"},"que1":null,"ans1":null,"message":null,"fielderrors":null}

i not able figure out how should parse object. tried using below code not jsonarray throws exception.

string getparam(string code, string element){ try {        string base = this.getitembyid(code);        jsonobject product = new jsonobject(base);       jsonarray jarray = product.getjsonarray("item");      string param =  jarray.getjsonobject(0).getstring("name");  return param; } catch (jsonexception e) {     e.printstacktrace();     return "error"; } } 

i recommend use sites json formater show types of json elements http://json.parser.online.fr/ , can use gson library parsing json using pojo class

  public class secquelist {    public string que1;    public int ans1;   public string message;   public string nextpage;   public questionlist secquelist;   }  public class questionlist {   @serializedname("1")   public string ques1;    @serializedname("2")   public int ques2;   @serializedname("3")  public string ques3;   @serializedname("4")  public string ques4;   @serializedname("5")  public string ques5;   @serializedname("6")  public string ques6;   @serializedname("7")  public int ques7;   @serializedname("8")  public string ques8;   @serializedname("9")  public string ques9;   @serializedname("10")  public string ques10;   @serializedname("11")  public string ques11;   } 

or can use parse using built in json object

  string jsonbody = string want parse   jsonobject quesjsonbody = new jsonobject(jsonbody);   jsonobject quesjson = quesjsonbody.getjsonobject("secquelist");   string quesjson1 =  quesjson.getstring("1");   string quesjson2 =  quesjson.getstring("2");   string quesjson3 =  quesjson.getstring("3");   string quesjson4 =  quesjson.getstring("4");   string quesjson5 =  quesjson.getstring("5");   string quesjson6 =  quesjson.getstring("6");   string quesjson7 =  quesjson.getstring("7");   string quesjson8 =  quesjson.getstring("8");   string quesjson9 =  quesjson.getstring("9");   string quesjson10 = quesjson.getstring("10");   string quesjson11 = quesjson.getstring("11");    string que1 = quesjsonbody.getstring("que1");   string ans1 = quesjsonbody.getstring("ans1");   string message = quesjsonbody.getstring("message");   string fielderrors = quesjsonbody.getstring("fielderrors"); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -