Android – create nested JSON objects

I tried to create a complex nested object as follows:

{
   "user":{
      "type":"facebook",
      "first_name":"harsha",
      "last_name":"mv",
      "gender":"male"
   },
   "friends":[
      {
         "id":"23",
         "name":"Vachana"
      },
      {
         "id":"23",
         "name":"Jyosna"
      },
      {
         "id":"23",
         "name":"Avinash"
      }
   ]
}

Android java code:

    JSONObject user = new JSONObject();
    try {
        user.put("first_name", "harsha");
        user.put("last_name", "mv");
        user.put("gender", "mail");

        System.out.println(user);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

I want to nest other objects, as shown above. Can someone tell me how to do it? What features or formats do I need to use?

resolvent:

'friends' is a jsonarray, and each item is a jsonobject. Just use put with jsonobject or jsonarray as the value. For an array, you must also specify an index

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>