0

Edited for clarification, I have below code:

Map<String, Object> map = new HashMap<String, Object>();
JSONArray jsonArray = new JSONArray();
// Assume there are 2 persons
for(Person person: persons) {
Map<String, Object> mapPerson = new HashMap<String, Object>();
mapPerson.put("name", "name");
mapPerson.put("age", "age");
jsonArray.put("", mapPerson);
}
map.put("", jsonArray);

I want the value of object map to be:

[
{
"name":"name",
"age":"age"
},
{
"name":"name",
"age":"age"
}
]

but it is:

{=
[
{
"name":"name",
"age":"age"
},
{
"name":"name",
"age":"age"
}
]
}

How can I achieve the above? Thanks

drn
  • 21
  • 6
  • The json you're showing is an array with 2 objects in it. So in java you'd need to put those two objects into an `ArrayList` instead of a `HashMap` – Lino Apr 06 '20 at 11:37
  • Yes that's right, I already thought about it, but in our legacy code, we pass hashmap as parameter of our sendrequest() method when sending request to apis. So I have no intention to change that cause other applications using it might be affected. So I want to still use hashmap. – drn Apr 06 '20 at 11:49
  • Does this answer your question? [Convert a JSON String to a HashMap](https://stackoverflow.com/questions/21720759/convert-a-json-string-to-a-hashmap) – hamidreza75 Apr 06 '20 at 11:50
  • No, what I need is the value of my hashmap should be:[{"name"="name","age"=20"}, {"name"="name2","age"=20"}] – drn Apr 07 '20 at 06:06

0 Answers0