0

私のコードは次のとおりです。

 JSONObject jChild=new JSONObject();
         JSONObject jParent=new JSONObject();
            for (Product p : boxAdapter.getBox()) {
              if (p.checked){
                try {
                    jChild.put("uid", p.uid);
                list.add(String.valueOf(jChild));

                    //list.add(String.valueOf(jParent));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
              }
            }
            jParent.put("users", list);

          // Toast.makeText(this, ""+jParent, Toast.LENGTH_LONG).show();
            Log.v("TakeAttendance","JSONpARENT "+String.valueOf(jParent));

出力:

{"users":"[{\"uid\":\"4\"}, {\"uid\":\"5\"}, {\"uid\":\"6\"}]"}

私が実際に必要なもの:

  {users: [
    {
    name: "acx",
    uid: "2"
    },

    {
    name: "test",
    uid: "6"
    },

    {
    name: "ccc",
    uid: "11"
    }
    ]
    }
4

4 に答える 4

1

JSON では、キーと値の両方が文字列である必要があります。プリティ プリント JSON オブジェクトが必要な場合は、pretty-print-json-in-java を試してください。

于 2013-09-05T05:14:53.693 に答える
0

リストがJSONArray..

         JSONObject jParent=new JSONObject();
         JSONArray list = new JSONArray();
         try {
            for (Product p : boxAdapter.getBox()) {                  
              if (p.checked){
                JSONObject jChild=new JSONObject(); //Correction here   
                    jChild.put("uid", p.uid);
                list.add(jChild);        //Correction here                       

              }
            }
            jParent.put("users", list);    
           } catch (JSONException e) {
                    e.printStackTrace();
                }

            Log.v("TakeAttendance","JSONpARENT "+jParent); //Correction here
于 2013-09-05T04:51:39.043 に答える