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))); }