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,318
  • 7
  • 39
  • 63
  • 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

92

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
  • 6,535
  • 2
  • 31
  • 46
aromero
  • 24,833
  • 6
  • 49
  • 76