2013-08-02 81 views
2

我有這樣的:JSON響應如何將JSON數組存儲在JSON數組結果中並顯示在列表視圖中?

{ 
    "ResponseCode": "000", 
    "ResponseDescription": "Successful", 
    "ResponseData": [ 
     [ 
      "RequestID", 
      "ProviderAuditID", 
      "RequestTime", 
      "Product", 
      "ProductCode", 
      "Currency", 
      "TopUpValue", 
      "TopUpValueRes", 
      "TopUpValuePro", 
      "TransactionResponseCode", 
      "TransactionResponseDescription" 
     ], 
     [ 
      "94", 
      "663", 
      "2013-08-02 07:02:54", 
      "Test 1", 
      "2222", 
      "IND", 
      "700.000000", 
      "700.000000", 
      "700.000000", 
      "000", 
      "Successful" 
     ], 
     [ 
      "93", 
      "661", 
      "2013-08-02 06:21:13", 
      "Test 2", 
      "5555", 
      "IND", 
      "160.000000", 
      "160.000000", 
      "160.000000", 
      "000", 
      "Successful" 
     ] 
    ] 
} 

我使用下面的機制::

final JSONObject jsonObject=new JSONObject(response); 

     if(jsonObject.getString("ResponseCode").equals("000")) 
     { 
         System.out.println("%%%% m inside...."); 

         JSONArray jArray = jsonObject.getJSONArray("ResponseData"); 
         System.out.println(" &&&&&&& ====>"+jArray.length()); 

         for (int i = 0; i < jArray.length(); i++) 
         { 
          JSONArray arrValues = (JSONArray) jArray.get(i); 

          System.out.println(" &&&&&&& Arr ====>"+arrValues); 

          for (int j = 0; j < arrValues.length(); j++) 
          { 
           String value = (String) arrValues.get(j); 

           System.out.println(" &&&&&&& ArrValues====>"+value); 
          } 
         } 
} 

測井響應:::

Arr ====>["RequestID","ProductType","RequestType","ProviderAuditID","RequestTime","Product","ProductCode","Currency","Batch","ReferenceNumber","TopUpValue","TopUpValueRes","TopUpValuePro","TransactionResponseCode","TransactionResponseDescription"] 

    ArrValues====>RequestID 

     ArrValues====>ProductType 

     ArrValues====>RequestType 

     ArrValues====>ProviderAuditID 

     ArrValues====>RequestTime 

     ArrValues====>Product 

     ArrValues====>ProductCode 

     ArrValues====>Currency 

     ArrValues====>Batch 

     ArrValues====>ReferenceNumber 

     ArrValues====>TopUpValue 

     ArrValues====>TopUpValueRes 

     ArrValues====>TopUpValuePro 

     ArrValues====>TransactionResponseCode 

     ArrValues====>TransactionResponseDescription 



Arr ====>["381","1","2","662","2013-08-02 07:01:08","Test 1","7878","IND","Test 1","12121212121","1600.000000","1600.000000000000","1600.000000","000","Successful"] 


     ArrValues====>381 

     ArrValues====>1 

     ArrValues====>2 

     ArrValues====>662 

     ArrValues====>2013-08-02 07:01:08 

     ArrValues====>Test 1 

     ArrValues====>7878 

     ArrValues====>IND 
     ArrValues====>Test 1 
     ArrValues====>12121212121 

     ArrValues====>1600.000000 

     ArrValues====>1600.000000000000 

     ArrValues====>1600.000000 

     ArrValues====>000 

     ArrValues====>Successful 



Arr ====>["380","0","1","657","2013-08-02 06:00:46","Test 1","9696","IND","Test 1","1234","900.000000","900.000000000000","900.000000","000","Successful"] 


     ArrValues====>380 

     ArrValues====>0 

     ArrValues====>1 

     ArrValues====>657 

     ArrValues====>2013-08-02 06:00:46 

     ArrValues====>Test 2 
     ArrValues====>9696 

     ArrValues====>IND 
     ArrValues====>Test 2 
     ArrValues====>1234 

     ArrValues====>900.000000 

     ArrValues====>900.000000000000 

     ArrValues====>900.000000 

     ArrValues====>000 

     ArrValues====>Successful 
成功解析此JSON

所需OutPut :::

RequestID :: 381 
Product :: Test 1 
Currency :: IND 
TransactionResponseDescription :: Successful 

RequestID :: 380 
Product :: Test 2 
Currency :: IND 
TransactionResponseDescription :: Successful 

編輯::

對我來說,存儲數據的問題是,我不明白請求ID或產品的整個陣列。 在第一個數組中,我得到了所有標題(標號),在其餘數組中我獲得了這些值。

所以,我知道在0 th索引我總是得到RequestID,依此類推。那麼,如何存儲這個值來達到理想的輸出呢?

+0

你想知道如何解析json? – Raghunandan

+0

@Raghunandan號我現在有分析結果,我想存儲它。 – AndroidLearner

+0

@Raghunandan請檢查編輯.. – AndroidLearner

回答

1
ArrayList<String> desc=new ArrayList<String>(); 
ArrayList<String> prods=new ArrayList<String>(); 
SparseArray<ArrayList<String>> vals=new SparseArray<ArrayList<String>>(); 

JSONArray arrValues = (JSONArray) jArray.get(0);// first array, descriptions 

System.out.println(" &&&&&&& Arr ====>"+arrValues); 

for (int j = 0; j < arrValues.length(); j++) 
{ 
    String value = (String) arrValues.get(j); 
    desc.add(value); 
    System.out.println(" &&&&&&& ArrValues====>"+value); 
} 

System.out.println(" &&&&&&& desc ====>"+desc);//IMPORTANT. CHECK THE POSITIONS 

for (int i = 1; i < jArray.length(); i++) 
{ 
    JSONArray arrValues = (JSONArray) jArray.get(i);// second array onwards, values 
    prods=new ArrayList<String>(); 
    System.out.println(" &&&&&&& Arr ====>"+arrValues); 

    for (int j = 0; j < arrValues.length(); j++) 
    { 
     String value = (String) arrValues.get(j); 
     prods.add(value); 
     System.out.println(" &&&&&&& ArrValues====>"+value); 
    } 
    vals.put(i-1, prods); 
} 

// now, 'desc' & 'vals' will do it. Eg:- 
for(int i=0;i<vals.size();i++){ 
    System.out.println("\n"+desc.get(0)+" ::"+vals.get(i).get(0));// for requestID 
    System.out.println(desc.get(3)+" ::"+vals.get(i).get(3));// for product 
System.out.println(desc.get(5)+" ::"+vals.get(i).get(5));//for currency, and so on.. 
    } 
+0

但是,一旦我們存儲它,如何檢索所有記錄? – AndroidLearner

+0

看最後一行。 'desc'會給出標題和'vals.size();'會說,有多少產品在那裏。在示例代碼中獲取其值。 – Nizam

+0

你能告訴我一些示例代碼嗎? – AndroidLearner

0

只要創建類JsonHolder這樣的:

public class JsonHolder{ 
    int RequestID; 
    String Product; 
    String Currency; 
    String etc... 
} 

只是在你的圈子,在這裏你解析JSON,解析每一個JSON數組項目後,然後創建JsonHolder的對象的ArrayList

ArrayList<JsonHolder>data = new ArrayList(); 

然後,創建JsonHolder對象並將其添加到ArrayList數據。所以你將擁有像自定義對象的所有json對象,並且將存儲在ArrayList中。