48

I wish to create json looking like:

{"man": 
   {
   "name":"emil",
   "username":"emil111",
   "age":"111" 
   }
}

within android. This is what I have so far:

JSONObject json = new JSONObject(); 
json.put("name", "emil"); 
json.put("username", "emil111"); 
json.put("age", "111"); 

Can anyone help me?

santBart
  • 2,396
  • 7
  • 41
  • 65
  • JSONObject json = new JSONObject(); json.put("name", "emil"); json.put("username", "emil111"); json.put("age", "111"); – santBart Jan 02 '12 at 22:41

1 Answers1

93

You can put another JSON object inside the main JSON object:

JSONObject json = new JSONObject();
JSONObject manJson = new JSONObject();
manJson.put("name", "emil");
manJson.put("username", "emil111");
manJson.put("age", "111");
json.put("man",manJson);
Smar
  • 7,223
  • 2
  • 34
  • 46
aromero
  • 25,403
  • 6
  • 54
  • 78