1

I have a JSONObject and I need to place inside a JSONArray.

This is my code:

ArrayList<Map<String, String>> Data = new ArrayList<>();
Map<String,String> element1 = new HashMap<>();
String JSONString = "{\"optGroups\":{\"Information\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"},\"freeToUse\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"}},\"optInsights\":{\"RemindPaymentTransfer\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"},\"BalanceIsTooLow\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"}}}";
element1.put("key1",JSONString);
Data.add(element1);
return new JSONArray(Data);

When I print the JSONArray I see the original String with "\" symbols.

How to fix the formatting so I will keep the JSONArray but see a valid JSONObject with "key":"value"?

Abdul Alim Shakir
  • 1,128
  • 13
  • 26
Tal Angel
  • 1,301
  • 3
  • 29
  • 63

2 Answers2

1

Try this maybe:

s.replaceAll("\\\\", ""); 
amer
  • 1,528
  • 1
  • 14
  • 22
1
JSONObject jsonObject= json.getJSONObject("{\"optGroups\":{\"Information\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"},\"freeToUse\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"}},\"optInsights\":{\"RemindPaymentTransfer\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"},\"BalanceIsTooLow\":{\"optStatus\":false,\"updateDate\":\"01/03/2017 01:16:03\"}}}");

Iterator x = jsonObject.keys();
JSONArray jsonArray = new JSONArray();

while (x.hasNext()){
    String key = (String) x.next();
    jsonArray.put(jsonObject.get(key));
}
bayrem404
  • 76
  • 2
  • 6