Monday 15 September 2014

android - How to pass bulk of json objects into Json array -



android - How to pass bulk of json objects into Json array -

hi can 1 help issue, not able pass arraylist of values in bellow json array.

please help me.

ex values:

arraylist<string> list_values = new arraylist<string>(); list_values.add(111); list_values.add(222); list_values.add(333); arraylist<string> list_labeltext = new arraylist<string>(); list_labeltext.add("mileage entry"); list_labeltext.add("tip amount"); list_labeltext.add("travel time"); arraylist<string> list_optionid = new arraylist<string>(); list_optionid.add("1"); list_optionid.add("2"); list_optionid.add("3");

i want pass above array list values in json array this:

clockoutemployeereponse":[{"response":[{"response":[{"value":"111"}],"labeltext":"mileage entry","clockoutoptionid":2},{"response":[{"value":"222"}],"labeltext":"tip amount","clockoutoptionid":4},{"response":[{"value":"333"}],"labeltext":"travel time","clockoutoptionid":3}]

my code follows:

jsonobject parentdata = new jsonobject(); jsonarray req_parameters = new jsonarray(); jsonobject req_clockout_obj = new jsonobject(); jsonarray req_arr = new jsonarray(); jsonobject value = new jsonobject(); req_clockout_obj.put("clockoutoptionid", 1); req_clockout_obj.put("labeltext", labeltext); (int = 0; < list_values.size; i++) { if (i == 0) { value.put("value", list_values.get(0)); req_arr.put(value); req_clockout_obj.put("response", req_arr); } else if (i == 1) { value.put("value", list_values.get(1)); req_arr.put(value); req_clockout_obj.put("response", req_arr); } else if (i == 2) { value.put("value", list_values.get(2)); req_arr.put(value); req_clockout_obj.put("response", req_arr); } } req_parameters.put(req_clockout_obj); parentdata.put("clockoutemployeereponse", req_parameters);

best way implement using model class , fetch info in instance of model(pojo) convert json string. need gson library. steps :       1. create pojo(model class) of according json format.       2. fetch info in object of pojo.       3. u can convert pojo's object json using

dataobject obj = new dataobject(); gson gson = new gson(); // convert java object json format, // , returned json formatted string string json = gson.tojson(obj);

please follow next link : http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

please utilize this, if need help allow me know.

android json arraylist arrays

No comments:

Post a Comment