Parsing Json string Help required

{"sessid":"Vn1qRrhZWUQxaF4Vq3AS-TSaCHnDwgJa8dYtI7ER_Xs",
"session_name":"SESS7b83e2fe2bcfd6997077715ad8799a43",
"user_product":[{"id":"19","uid":"1","created":"1336803149","modified":"1336803149",
"subject":"Patras Bukhari",]}}

Blockquote i have this json in a sting and i want to fetch data from the arrayy user_product i have tried alot but unable to do that . And my project is on Android. help is appreciated in advance thanks

Answers


check following code

 String a = {"sessid":"Vn1qRrhZWUQxaF4Vq3AS-TSaCHnDwgJa8dYtI7ER_Xs",
 "session_name":"SESS7b83e2fe2bcfd6997077715ad8799a43",
 "user_product":[{"id":"19","uid":"1","created":"1336803149","modified":"1336803149",
 "subject":"Patras Bukhari",]}}

 JSONObject json1 = new JSONObject(a);
 JSONArray userProduct = json1.getJSONArray("user_product");
 for(int i = 0; i < userProduct.length();i++){
   JSONObject tempJSON = userProduct..getJSONObject(i);
   String id = tempJSON.getString("id");
   String uid = tempJSON.getString("uid");
   String created = tempJSON.getString("created");
   String modified = tempJSON.getString("modified");
   String subject = tempJSON.getString("subject");
 }

Hope this will solve your problem


Is the above JSON the real one you are using? If it was, the format is not valid. I guess you might encounter some exceptions working with it. Regardless to the validity, you can get user_product array by

JSONObject source = new JSONObject(your_source);
JSONArray userProducts = source.getJSONArray("user_product");
for(int i = 0; i < userProducts.length(); i++){
  JSONObject product = userProducts.getJSONObject(i);
  int uid = product.getInt("uid"); //get the uid as integer
}

Need Your Help

Tool for creating Vagrantfile

regex bash shell scripting vagrant

I am using Vagrant and I will be creating Vagrantfile's for a number of services. There is a lot of overlap in what I have to do to create those Vagrantfiles so I would like automate some of it.

AngularJS' ng:areq Bad Argument "{controller} is not a function", with textAngular on CodeIgniter

javascript php angularjs codeigniter-3 textangular

I'm trying to implement a text editor with textAngular in a CodeIgniter view, but it keep returning this error: