Convert ArrayList of HashMap into JSON String
23,568
There are many ways to achieve that. I will explain two ways:
You can do like this:
ArrayList<HashMap<String, String>> dataMap = new ArrayList<HashMap<String, String>>();
HashMap<String, String> data1 = new HashMap<String, String>();
data1.put("image", "2130837526");
data1.put("category", "Chairs");
data1.put("Quantity", "1");
HashMap<String, String> data2 = new HashMap<String, String>();
data2.put("image", "2130837566");
data2.put("category", "Mirrors");
data2.put("Quantity", "2");
dataMap.add(data1);
dataMap.add(data2);
List<JSONObject> jsonObj = new ArrayList<JSONObject>();
for(HashMap<String, String> data : dataMap) {
JSONObject obj = new JSONObject(data);
jsonObj.add(obj);
}
JSONArray test = new JSONArray(jsonObj);
System.out.println(test.toString());
Or you can use Jackson Object Mapper:
Add external libraries to Eclipse Project
Java Code
ArrayList<HashMap<String, String>> dataMap = new ArrayList<HashMap<String, String>>();
HashMap<String, String> data1 = new HashMap<String, String>();
data1.put("image", "2130837526");
data1.put("category", "Chairs");
data1.put("Quantity", "1");
HashMap<String, String> data2 = new HashMap<String, String>();
data2.put("image", "2130837566");
data2.put("category", "Mirrors");
data2.put("Quantity", "2");
dataMap.add(data1);
dataMap.add(data2);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(dataMap);
System.out.println(json);
Both options will print:
[{"image":"2130837526","category":"Chairs","Quantity":"1"},{"image":"2130837566","category":"Mirrors","Quantity":"2"}]
Related videos on Youtube
Author by
Infinity
Updated on March 14, 2020Comments
-
Infinity about 4 years
I want to convert
ArrayList<Hashmap<String,String>>
toJSONArray
. I usedJSONArray array = new JSONArray(array_list);
but it does not work properly it returns
["{image=2130837526, category=Chairs, Quantity=1, price=400, name=chair, prodId=34}","{image=2130837566, category=Mirrors, Quantity=1, price=3000, name=La Fonda, prodId=35}"]
Instead it should return
cartitems=[{"name":"Chair","price":"1001","prodId":"2","category":"Chairs","image":"2130837519","Quantity":"1"},{"name":"Baxton Studio Club Chair","price":"4545","prodId":"5","category":"Chairs","image":"2130837521","Quantity":"1"}]
-
OneCricketeer about 8 yearsThat first JSON block is just an array of strings... Are you sure you can pass an Arraylist into the constructor of JSONArray like that? Looks like it just toString'd every element of the list
-
Infinity about 8 years@cricket_007 I am not sure about it becase it work for simple ArrayList<string> but not working for ArrayList<hashmap<string,string>>
-
Kostas Drak about 8 yearswhy dont you post your Arraylist here?
-
OneCricketeer about 8 yearsIf I had to guess, any type such as int, string, and boolean can be used to construct a JSONArray from a list because those are JSON datatypes. What you need to do is loop over your Arraylist, and create a new JsonObject from each Hashmap, then add each of those to the JSONArray
-
-
Infinity about 8 yearswhere i need to write For Maven code. Actually i am new to android programming @andrebonna
-
André Bonna about 8 yearsIs your IDE Android Studio?
-
André Bonna about 8 yearsTo add external libraries to Android Studio: stackoverflow.com/questions/16608135/…
-
Infinity about 8 yearsi am using Eclipse IDE @andrebonna
-
André Bonna about 8 yearsOK, I changed my answer to help you with eclipse
-
André Bonna about 8 yearsI added another way to achieve that too.
-
Infinity about 8 yearsThanks alot for your cooperation. Tommorow is my project submission day and you saved my project. I don't have words to thanks you. THANKS ALOT. God Bless you
-
Infinity about 8 yearsLet us continue this discussion in chat.