在Android中使用Json数组创建JSON对象

时间:2013-12-27 10:53:15

标签: android json

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

代码部分:

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

我想用android中的上述格式创建带有Json Array的Json Object。我已经尝试了很多东西,但没有得到如上所述。

2 个答案:

答案 0 :(得分:0)

请参阅代码here。这与您尝试实现的示例相同。

答案 1 :(得分: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);

这是您的格式