sankardev51
sankardev51

Reputation: 37

Converting Map to JSONObject

import org.json.JSONArray;
import org.json.JSONObject;

    HashMap<String,String> testAttMap = new HashMap<String,String>();
        HashMap<String,String> jsonMap = new HashMap<String,String>();
        jsonMap.put("containerType", "Drive");
        testAttMap.put("idNbr", "11111111111");
        testAttMap.put("name", "ATTTT");
        jsonMap.put("testAtts", new JSONObject(testAttMap).toString());
        System.out.println(new JSONArray().put(jsonMap));   

Expecting :

[{"containerType":"Drive","testAtts":"{"idNbr":"11111111111","name":"ATTTT"}"}]

Actual Result :

[{"containerType":"Drive","testAtts":"{\"idNbr\":\"11111111111\",\"name\":\"ATTTT\"}"}]

Can anyone suggest a fix?

Upvotes: 0

Views: 3433

Answers (1)

Alexander Mills
Alexander Mills

Reputation: 99960

What you want to do is simply:

jsonMap.put("testAtts", new JSONObject(testAttMap));

instead of

jsonMap.put("testAtts", new JSONObject(testAttMap).toString());

the slashes are there because you are escaping the double quotes

Upvotes: 2

Related Questions