1

I'm using renderJSON() method to return an array of objects, with a custom JsonSerializer for the class of each element. The response has the following format:

[{"id":2,"name":"fred"},{...},...]

But I would like to add the name of the array at the begginning:

"arrayname":[{"id":2,"name":"fred"},{...},...]

How can I add the name of the object to a JSON response with renderJSON()? Should I use a template or can I do it with java code?

0

2 Answers 2

3
"arrayname":[{"id":2,"name":"fred"},{...},...]

looks like invalid Json, but if you wrap the array in an object you could get:

{"arrayname":[{"id":2,"name":"fred"},{...},...]}

E.g. something like this:

public class MyArrayWrapper {
    public final User[] arrayname;
    public MyArrayWrapper(User[] arrayname) {
        this.arrayname = arrayname;
    }
}

Then you can call renderJSON(new MyArrayWrapper(yourUserArray)); to get JSON like:

{"arrayname":[{"id":2,"name":"fred"},{...},...]}
Sign up to request clarification or add additional context in comments.

Comments

0

Or just return as a list Arrays.asList(arr);

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.