简体   繁体   中英

Json object inside array inside json object using kotlin or java

I would like to put a json object inside an array, inside another json object using java or kotlin. Something like this:

{
  "strName" : "hello world"
  "arrAddress" : [
    {
      "strAddress" : "52 street",
      "strPincode" : "683543"
    }
  ]
}

I have looked into other questions, but I couldn't find suitable answers.

I found the answer if anyone wants to use JSON objects like these:

JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("strName", "hello world");
        jsonObject.add("arrAddress", new JsonParser().parse(
                "[\n" +
                "    {\n" +
                "      \"strAddress\" : \"52 street\"\n," +
                "      \"strPincode\" : \"683543\"\n" +
                "    }\n" +
                "  ]"
        ));

Output:

{"strName":"hello world","arrAddress":[{"strAddress":"52 street","strPincode":"683543"}]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM