2

I have the JSON Response like this ::

{
    "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"
        ]
    ]
}

I am successfully parsed this JSON using the below mechanism ::

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

Log Response :::

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

Desired OutPut :::

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

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

EDIT ::

The problem for me to store the data is that I don't get the whole array of Request ID or Product. In first array I got all the headers (label) and in the rest of arrays I got the values.

So, I know that at 0th index I always got the RequestID, and so on. So, how to store the value to achieve the desired output?

2
  • you want to know how to parse the json? Commented Aug 2, 2013 at 14:16
  • @Raghunandan No. I have the parsed result now i want to store it . Commented Aug 2, 2013 at 14:18

2 Answers 2

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..
     }
Sign up to request clarification or add additional context in comments.

3 Comments

But how to retrive all the records once we store it ?
Look at last lines. 'desc' will give headings and vals.size(); will say, how many products are there. Get its values as in the sample code.
Edited answer. Check it
0

Just create class JsonHolder like this:

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

Then create ArrayList of JsonHolder's objects

ArrayList<JsonHolder>data = new ArrayList();

Then just in your circle, where you parse json, after parsing every json array item, create JsonHolder object and add it to ArrayList data. So you will have all your json objects like custom objects and the will be store in ArrayList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.