如何在Android中使用JSONARRAY检索多个记录

时间:2017-06-29 08:45:44

标签: android

Now currently i am getting only updated data of same user by passing id through url.

使用以下代码......

 try {

             JSONArray jr = new JSONArray(jsonStr);
            for(i=0;i<3;i++) {
                JSONObject jb = (JSONObject) jr.getJSONObject(i);

                value = jb.getString("amount");
                tran_count = jb.getString("tran_count");
                debitstr = jb.getString("debit");
                creditstr = jb.getString("credit");

                pointsmodestr = jb.getString("points_mode");

                updatecpa = value;
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }

 But i need to retrieve multiple data of same user id.

例如:     现在对于上面的代码,它只显示同一用户的更新数据。     它只显示一个数组。

[{"id":"46097","user_id":"1066","email":"rahul@gmail.com","rolename":"12","account_no":"3445557752334556","debit":"0","credit":"0","amount":"0","points_mode":"wallet","challan":"","used":"yes","paid_to":"00","pay_type":"0","tranx_id":"One time Sponsorship Charges Deduction","active":"0","created_at":"1497008167","modified_at":"1497008167","tran_count":"3"}]

如何在同一用户的android中显示多个JSONARRAY

示例:

1st array of user id=1066    [{"id":"46097","user_id":"1066","email":"rahul@gmail.com","rolename":"12","account_no":"3445557752334556","debit":"0","credit":"0","amount":"0","points_mode":"wallet","challan":"","used":"yes","paid_to":"00","pay_type":"0","tranx_id":"One time Sponsorship Charges Deduction","active":"0","created_at":"1497008167","modified_at":"1497008167","tran_count":"3"}] 

第二个用户ID数组= 1066

[{"id":"27098","user_id":"1066","email":"rahul@gmail.com","rolename":"12","account_no":"3445557752334556","debit":"0","credit":"0","amount":"0","points_mode":"wallet","challan":"","used":"no","paid_to":"00","pay_type":"2","tranx_id":"Referral offer","active":"0","created_at":"1492671459","modified_at":"1492671459","tran_count":"2"}]

1 个答案:

答案 0 :(得分:0)

在代码中,只有值被添加到列表中,为什么?     希望jsonStr应该是正确的

 JSONArray jr = new JSONArray(jsonStr);

代码将获得jsonArray的类型。如果是这种情况,它将被完美检索。它将采用这种格式

`[{"id":"46097","user_id":"1066","email":"rahul@gmail.com","rolename":"12","account_no":"3445557752334556","debit":"0","credit":"0","amount":"0","points_mode":"wallet","challan":"","used":"yes","paid_to":"00","pay_type":"0","tranx_id":"One` time Sponsorship Charges Deduction","active":"0","created_at":"1497008167","modified_at":"1497008167","tran_count":"3"},
     {"id":"27098","user_id":"1066","email":"rahul@gmail.com","rolename":"12","account_no":"3445557752334556","debit":"0","credit":"0","amount":"0","points_mode":"wallet","challan":"","used":"no","paid_to":"00","pay_type":"2","tranx_id":"Referral offer","active":"0","created_at":"1492671459","modified_at":"1492671459","tran_count":"2"},<another value>] 

在您的代码中,而不是

  for(i=0;i<3;i++) {
    please change to
    for(i=0;i<jr.length();i++) {  //as till the last jsonarray it will traverse.

如果事情不起作用,大多数jsonStr一定是错的 希望有所帮助