0

my code is:

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

Output :

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

What i actually need :

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

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

    {
    name: "ccc",
    uid: "11"
    }
    ]
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Madhav Bhattarai
  • 847
  • 2
  • 10
  • 29
  • You're nowhere putting the `name` attribute so obviously your output does not have it – Niks Sep 05 '13 at 06:25

4 Answers4

1

JSON requires both key and value are strings. If you need pretty print JSON object, try pretty-print-json-in-java

Community
  • 1
  • 1
Michael SM
  • 715
  • 3
  • 11
  • 25
0

If list is 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
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • JSON requires both key and value are strings. If you need pretty print JSON object, try [pretty-print-json-in-java](http://stackoverflow.com/questions/4105795/pretty-print-json-in-java) – Michael SM Sep 05 '13 at 05:11
0

Correct JOSNObject will be

     {"users": [

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

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

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

}

String data ; // JOSN String
JSONObject data = new JSONObject(data);
JSONArray array = data.getJSONArray("users");
for(int i=0; i<array.length; i++){
        JSONObject obj = array.getJSONObject(i);
        String name = obj.getString("name");
        String uid  = obj.getString("uid");
}

For generating from Data

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

                }
                catch (JSONException e) {
                    e.printStackTrace();
                }

              }
            }
            jParent.put("users", list);
strike
  • 1,031
  • 8
  • 24
0

What you are getting is actually correct JSON. It will be parsed back to a JSON object correctly. If you want this only for printing purpose, you can just replace '\"' with blank string.

TechSpellBound
  • 2,505
  • 6
  • 25
  • 36