1

I am using the Google GSON library to convert an ArrayList of countries into JSON:

ArrayList<String> countries = new ArrayList<String>();

// arraylist gts populated

Gson gson = new Gson();
String json = gson.toJson(countries);

Which yields:

["AFGHANISTAN","ALBANIA","ALGERIA","ANDORRA","ANGOLA","ANGUILLA","ANTARCTICA","ANTIGUA AND BARBUDA","ARGENTINA","ARMENIA","ARUBA","ASHMORE AND CARTIER ISLANDS","AUSTRALIA","AUSTRIA","AZERBAIJAN"]

How can I modify my code to generate a JSON Array? For example:

[  
   {  
      "AFGHANISTAN",
      "ALBANIA",
      "ALGERIA",
      "ANDORRA",
      "ANGOLA",
      "ANGUILLA",
      "ANTARCTICA",
      "ANTIGUA AND BARBUDA",
      "ARGENTINA",
      "ARMENIA",
      "ARUBA",
      "ASHMORE AND CARTIER ISLANDS",
      "AUSTRALIA",
      "AUSTRIA",
      "AZERBAIJAN"
   }
]

Thanks!


Here is the code that my Java client uses to parse web service responses that already contain the curly-braces. This is why I want the countries response to contain the curly braces:

Gson gson = new GsonBuilder().create();
    ArrayList<Map<String, String>> myList = gson.fromJson(result,
            new TypeToken<ArrayList<HashMap<String, String>>>() {
            }.getType());

    List<Values> list = new ArrayList<Values>();

    for (Map<String, String> m : myList) {
        list.add(new Values(m.get(attribute)));
    }
3
  • 5
    [{"AFGHANISTAN","ALBANIA","ALGERIA","ANDORRA","ANGOLA","ANGUILLA","ANTARCTICA","ANTIGUA AND BARBUDA","ARGENTINA","ARMENIA","ARUBA","ASHMORE AND CARTIER ISLANDS","AUSTRALIA","AUSTRIA","AZERBAIJAN"}] isn't JSON. Commented Dec 21, 2012 at 15:00
  • hey what exactly are you trying to achieve ? Commented Dec 21, 2012 at 15:23
  • I'm just trying to get the original format ["AFGHANISTAN","ALBANIA","ALGERIA"] into a new format with the curly braces: [{"AFGHANISTAN","ALBANIA","ALGERIA"}], regardless of it not being JSON. Commented Dec 21, 2012 at 15:26

3 Answers 3

3

To build the string you show us, which isn't JSON at all, you may do this :

StringBuilder sb = new StringBuilder("[{");
for (int i=0; i<countries.size(); i++) {
    sb.append("\"").append(countries.get(i)).append("\"");
    if (i<countries.size()-1) sb.append(",");
}
sb.append("}]");
String theString = sb.toString();

I'd recommend not trying to use Gson, which is only dedicated to JSON.

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

Comments

1

Using curly-braces is not a "style", it is how JSON denotes an object. square brackets represent a list, and curly-braces are used to represent an object, which in Javascript behaves like a map. Putting curly braces around the list entries is nonsensical because within the object you need name-value pairs (just like a map).

1 Comment

Thanks, I was confused. You're right, I will either need to modify my web service to generate name-value pairs with curly-braces (which doesn't make sense for just a list of countries), or just add a special case to my java client to parse the JSON (what I have currently). I appreciate your help.
1

A simple text-representation of an array in JSON is exactly what Gson returns. What for do you need that curly-braces style?

1 Comment

I have several web services that return responses with the curly-braces style, so my Java client is set up to parse responses with the curly-braces. Now, I have a new web service that is using GSON and returning a response without curly-braces. I want to modify that code (shown above) to add curly braces, so that my Java client can parse it. I don't want to modify my Java client to parse different response formats.

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.