0

Already i return my data in json format but i want to convert it into nested json format data.

my code is written in spring such that is following

@GET
@Path("/findall")
public List<UserRole> getAll() {

             return userroleservice.findAll();
}

This program return the following output

[{"roleId":1,"roleName":"system Admin","createUser":"1","createActive":"N"},{"roleId":2,"roleName":"admin","createUser":"1","createActive":"Y"},{"roleId":3,"roleName":"system Admin","createUser":"1","createActive":"Y"}]

How to convert it to nested json format like

  [{ "id": 1, "data": "Category", "attr": { "id": "1", "selected": false }, "children":
                [
                    { "id": 2, "data": "SubCategory1", "attr": { "id": "2",      "selected": false }, "children":
                            [{ "id": 4, "data": "Item1", "attr": { "id": "4", "selected": false }, "children": [] },
                             { "id": 5, "data": "Item2", "attr": { "id": "5", "selected": false }, "children": [] },
                             { "id": 6, "data": "Item3", "attr": { "id": "6", "selected": false }, "children": [] }
                            ]
                    },

                    { "id": 3, "data": "SubCategory2", "attr": { "id": "3", "selected": false }, "children":
                             [{ "id": 7, "data": "Item4", "attr": { "id": "7", "selected": false }, "children": [] },
                              { "id": 8, "data": "Item5", "attr": { "id": "8", "selected": false }, "children": [] },
                              { "id": 9, "data": "Item6", "attr": { "id": "9", "selected": false }, "children": [] }
                             ]
                    }
                ]
}];

or How to return DB data in nested json format ??

Please anybody help.

1 Answer 1

1
  1. Create a new class that corresponds to the JSON format you want to have. Generate the desired java classes using JSON TO POJO link
  2. After you are done with the POJO generation write a translator that will translate the DB object to your new POJO object.
  3. Pass the same to next layer
Sign up to request clarification or add additional context in comments.

1 Comment

Please some coding for example...And can i append to Gson object?? like

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.