解析Json字符串需要帮助

时间:2012-05-18 11:13:11

标签: android json

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

Blockquote我有一个刺痛的json,我想从阵列user_product中获取数据   我已经尝试了很多,但无法做到这一点。我的项目是在Android上。   提前感谢帮助

2 个答案:

答案 0 :(得分:1)

检查以下代码

 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");
 }

希望这能解决您的问题

答案 1 :(得分:1)

上面的JSON是你真正使用的吗?如果是,则格式无效。我想你可能会遇到一些使用它的异常。无论有效性如何,您都可以通过

获取user_product数组
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
}