1

我正在制作 Json ,但我没有制作正确的格式它只给了我用户的第一个属性和最后一个属性项,没有给我完整的 json ..提前谢谢..我的代码是

JSONObject json = new JSONObject();

        try {
            JSONObject subJson = new JSONObject();
            //JSONArray jsonArray = new JSONArray();

            subJson.put("saleManId", LogIn.UserId);
            subJson.put("customerId", LogIn.CustomerId);

            json.put("User", subJson);

            for (int i = 0; i < LogIn.subValues.length; i++) {
                JSONObject subSubObj = new JSONObject();

                for (int j = 0; j < LogIn.subValues[i].length; j++) {
                    if (LogIn.subValues[i][j] > 0) {

                        JSONObject item = new JSONObject();

                        item.put("itemID", LogIn.subIds[i][j]);
                        item.put("qty", LogIn.subValues[i][j]);

                        subSubObj.put("item", item);

                    }

                }


                if(subSubObj.length()>0)
                {
                    json.put("orderDetail", subSubObj);
                    Log.d("json", json.toString());
                }


            }
            Log.d("jsonnnnnnnnnnnnnnnnnnnnnnnnnnnnn", json.toString());

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }  

它们是 Array 中的 30 到 35 个项目,但它只给了我这个

{"orderDetail":{"item":{"qty":1,"itemID":"60"}},"User":{"customerId":"460","saleManId":"1"}}
4

1 回答 1

0

我明白了... :) 谢谢大家...

JSONObject json = new JSONObject();

        try {
            JSONObject subJson = new JSONObject();
            JSONArray jsonArray = new JSONArray();

            subJson.put("saleManId", LogIn.UserId);
            subJson.put("customerId", LogIn.CustomerId);

            json.put("User", subJson);

            for (int i = 0; i < LogIn.subValues.length; i++) {



                for (int j = 0; j < LogIn.subValues[i].length; j++) {
                    if (LogIn.subValues[i][j] > 0) {

                        JSONObject subSubObj = new JSONObject();
                        JSONObject item = new JSONObject();

                        item.put("itemID", LogIn.subIds[i][j]);
                        item.put("qty", LogIn.subValues[i][j]);

                        subSubObj.put("item", item);

                        jsonArray.put(subSubObj);

                    }

                }


            }

            json.put("orderDetail", jsonArray);
            Log.d("jsonnnnnnnnnnnnnnnnnnnnnnnnnnnnn", json.toString());

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         sendString = json.toString();

输出结果是..

{"orderDetail":[
{"item":{"qty":1,"itemID":"4"}},
{"item":{"qty":1,"itemID":"14"}},
{"item":{"qty":1,"itemID":"28"}},
{"item":{"qty":1,"itemID":"59"}},
{"item":{"qty":1,"itemID":"60"}},
{"item":{"qty":1,"itemID":"62"}},
{"item":{"qty":1,"itemID":"24"}},
{"item":{"qty":1,"itemID":"36"}},
{"item":{"qty":1,"itemID":"51"}}],
"User":{"customerId":"414","saleManId":"1"}}
于 2013-03-19T10:24:19.160 回答