1

I have written a code, where I need to create a JSON format output from a "select * query". So far I am able to get the output like this, but I have to wrap it up inside "users"

{
  "displayName": "Tony Stark",
  "givenName": "Tony",
  "surname": "Stark"
},
{
  "displayName": "James Martin",
  "givenName": "James",
  "surname": "Martin"
}

I need something like this:

{
   "users": 
      [
            {
                  "displayName": "Tony Stark",
                  "givenName": "Toney",
                  "surname": "Stark"
            },
            {
                  "displayName": "James Martin",
                  "givenName": "James",
                  "surname": "Martin"
            }
      ]
}

Please help me out in this.

2
  • 3
    Please update the question with code where you are querying database and converting to json, Commented Aug 11, 2020 at 6:34
  • Does this answer your question? How to create correct JSONArray in Java using JSONObject Commented Aug 11, 2020 at 15:25

2 Answers 2

1

Yes it's possible if you create a model class named Users, map it properly along it with it's attributes so it will show exactly as shown by you in the screenshot. Attaching reference model class.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

List<JsonObject> jObj = getJSONFormat(resultSet);
I have a List as given above, I have the resultSet which is a List as well, if you print this you will get what I am getting now,
Can you tell me how can I wrap this list inside "users"
0

It would be easy if add a wrap class and convert it to json

import java.util.List;

public class UserWrap {
    private List<User> users;

    public UserWrap(List<User> users) {
        this.users = users;
    }
}

Comments

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.