0
       "ORDERDETAIL":[
            {
                "Description":"asdf",
                "Quantity":"5",
                "Unit Price":"77",
                "No.":"1",
                "Line No.":"100",
                "Document Type":"1",
                "Document No.":"109"
            },
            {
                "Description":"torage",
                "Quantity":"5",
                "Unit Price":"7",
                "No.":"19",
                "Line No.":"10",
                "Document Type":"1",
                "Document No.":"10"
            },
            {
                "Description":"IN",
                "Quantity":"5",
                "Unit Price":"7",
                "No.":"1",
                "Line No.":"1",
                "Document Type":"1",
                "Document No.":"9"
            }
        ]

Code Part:

ArrayList<String> arlstKeyDetail = new ArrayList<String>();
ArrayList<String> arlstValDetail = new ArrayList<String>();

JSONArray jsonSubDataArry = new JSONArray();
JSONObject jsonSubObj = new JSONObject();
JSONObject jsonTemp1 = new JSONObject();

Iterator iterKey = jsonArrDisplayRecord.getJSONObject(1).keys();
            while(iterKey.hasNext())
            {
                String key = (String)iterKey.next();
                arlstKeyDetail.add(key);
                arlstValDetail.add(jsonArrDisplayRecord.getJSONObject(1).getString(key));


            }

for(int i=0 ; i < arlstKeyDetail.size() ; i++)
            {
                for(int j=0; j <itLength; j++){
                    jsonSubObj.put(arlstKeyDetail.get(j), arlstValDetail.get(j));
                }
                jsonSubDataArry.put(jsonSubObj);
                jsonTemp1.put("ORDERDETAIL", jsonSubDataArry);
            }

I want to create Json Object with Json Array like above format in android. i already tried lots of thing but didnt get as like above.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99

2 Answers2

0

Refer to the code here. This has the same example that you are trying to achieve.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
0
JSONObject ORDERDETAIL = new JSONObject();

JSONArray array = new JSONArray();

JSONObject array_content1 = new JSONObject();
array_content1.put("Description","asdf");
array_content1.put("Quantity","5");
.....

JSONObject array_content2 = new JSONObject();
array_content1.put("Description","torage");
array_content1.put("Quantity","5");
.....

array.add(array_content1);
array.add(array_content2);

ORDERDETAIL.put("ORDERDETAIL",array);

This is your format

henry4343
  • 3,871
  • 5
  • 22
  • 30