I have created an ArrayList
in Java:
List<String> listValuesFromMap = new ArrayList<String>();
which I fill with values using entry.getValue()
method:
listValuesFromMap.add((String) entry.getValue().toString());
and I got something like this:
"names" : ["[name1, name2, name3]"]
How to remove these extra brackets with quotes to get only:
[name1, name2, name3]
Whole part of code:
@SuppressWarnings("unchecked")
Map<String, Object> attributes = (Map<String, Object>) input
.getValueByField(ATTRIBUTES_FIELD);
Document mongoDoc= new Document();
List<String> listValuesFromMap = new ArrayList<String>();
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
if (entry.getKey().contains("names")) {
JSONArray jsonArray = new JSONArray();
listValuesFromMap.add((String) entry.getValue().toString());
for (String item : listValuesFromMap) {
jsonArray.add(item.toString());
}
mongoDoc.put(entry.getKey(), jsonArray);
}
else {
listValuesFromMap.add((String) entry.getValue().toString());
mongoDoc.put(entry.getKey(), entry.getValue());
}
}
I think the problem is in the line listValuesFromMap.add((String) entry.getValue().toString());
- values from the entry.getValue()
are already in [] and add
method put extra []. Does anyone know how to avoid these extra brackets?