json没有给我竞争格式,

时间:2013-03-19 08:10:06

标签: android json

我正在制作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"}}

1 个答案:

答案 0 :(得分: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"}}
相关问题