java - 将 Map 转换为 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));
期待:
[{"containerType":"Drive","testAtts":"{"idNbr":"11111111111","name":"ATTTT"}"}]
实际结果 :
[{"containerType":"Drive","testAtts":"{\"idNbr\":\"11111111111\",\"name\":\"ATTTT\"}"}]
任何人都可以建议修复吗?
解决方案
你想做的很简单:
jsonMap.put("testAtts", new JSONObject(testAttMap));
代替
jsonMap.put("testAtts", new JSONObject(testAttMap).toString());
斜线在那里是因为你正在转义双引号
推荐阅读
- javascript - 如何在 React-native 中复制相同的 Android 加密方法?
- python - 如何从同一资源并发中多次绘制
- javascript - 当我按下“Enter”按钮时,需要一些帮助来为 keyup 添加 addEventListener
- c# - Response.Redirect 加载未完成
- intellij-idea - 发现多个 defaults.yaml 资源。智能理念
- javascript - 删除从 chrome 扩展中添加的事件侦听器?
- microsoft-graph-api - 如何通过 Microsoft Graph 创建 appRoleAssignment?
- google-api - 如何确定是哪个 gmail 权限导致 Google 向我的客户发送“限制访问您的 Google 帐户中的数据”的电子邮件?
- reactjs - 如何在 create-react-app 上使用“/”阻止路由
- php - 如何将日志发送到 efk 服务器