1
votes

In my project I convert list of maps to jsonArray. However, sometimes there are values which are maps, So I convert them to Strings then add them to the JSONArray. But when I do this, the apostrophes for each key and value is being replaced by backslash. For example, my expected result:

[{"user":"124545","count":"60"}]

actual result:

["{\"user\":\"124545\",\"count\":\"60\",}"]

My code snippet:

Map<String, String> map = row.getMap(i, String.class, String.class);
                        JSONObject objectMap= new JSONObject(map);
                        String cellData = objectMap.toString();
                        JSONArray array= new JSONArray();
                        array.put(cellData);

I'm running it through rows of query results of Cassandra database, so I have in the jsonArray regular values separated by comas, and sometimes map values.

Why are you adding objectMap.toString(); to the JSONArray rather than the objectMap itself? - Sotirios Delimanolis
It's a snippet code, I have many manipulations later, cellData string can be an integer or just a regular string. This is a condition when I encounter a map. At the end I put each cellData to list of maps, then return it as JSONArray - 123josh123
cellData string can be an integer or just a regular string cellData is the JSON string representation of objectMap. You're double encoding it to JSON by adding it to the array. Just use array.put(objectMap). - Sotirios Delimanolis
There are no apostrophes in your JSON. - khelwood
Is there a way to put the representation of jsonMap as String into jsonArray? - 123josh123