将Map转换为JSONObject

时间:2019-03-16 23:10:03

标签: java json

dz = hist.T.flatten()

期待:

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));   

实际结果:

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

有人可以提出修复建议吗?

1 个答案:

答案 0 :(得分:1)

您要做的只是:

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

代替

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

存在斜线是因为您要转义双引号