Abhishek Shivkumar Abhishek Shivkumar - 11 months ago 42
Java Question

How do you code the following Json object?

I want to create a Json object like the following:

{"name": "Maximum", "children": [
{"name": "where", "size": 299},
{"name": "xor", "size": 354},
{"name": "_", "size": 264}

Which library to use to create the above Json string and how should the code look like?

Answer Source

Try gson . It has good support for this. Check out this user guide.

Your class structure would look like:

class Parent{
        String name;
        Children[] children;
//getter and setter
    class Children{
        String name;
        int size;
//getter and setter

then in your code:

   Parent parent = new Parent();
    //poppulate parent object with required values 
    Gson gson = new Gson();