0

I have to return a JSON string in this format using Spring. My Spring controller will take care of this since I specify the return value as @ResponseBody. Now I have to construct the value in Java in the following format:

["Male", "m"]

Please help me to get this done in Java.

3
  • 5
    The question is "OK". The title is extremely bad. Commented Jun 26, 2013 at 6:14
  • 1
    Now the title is somehow OK, the question still "OK" :D Commented Jun 26, 2013 at 6:18
  • If I understand correctly, you need an object that can be marshalled to json by your spring response provider. Creating a json like array wouldnt help you much if that is the case. Commented Jun 26, 2013 at 6:19

2 Answers 2

1

Return String[] or List<String>. Either of these should work.

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

Comments

1
System.out.println(
       Arrays.toString(new String[] {"Male", "m"})); // prints: ["Male", "m"]

// if the data comes as a dynamic List
String list = Arrays.asList("Male", "m").toString();
System.out.println(list); // prints: ["Male", "m"]

1 Comment

You'd have to use Arrays.toString(Object[]) for the first example.

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.