ganji1 2016-01-21 03:19 采纳率: 100%
浏览 7103
已采纳

关于Json value值取消双引号问题

我现在需要获得一个Json,格式如下:
var data_d = {"id":"root","text":"根节点","isExpand":"true","childNodes":[{"id":"org1","text":"软件","showCheck":"true"}]};

其中:childNodes对应是一个数组,[{value}],需要这样的格式。
我在后台处理的代码如下:
Map map = new HashMap();
map.put("id", "root");
map.put("text", "根节点");
map.put("isExpand", "true");
map.put("childNodes", "["+hmap()+"]");

private static Map hmap(){
Map map = new HashMap();
map.put("id", "root");
map.put("text", "根节点");
map.put("showCheck",true);
return map;
}

String map2 = mapToJson(map);

打印出来的结果是:
[json]: [{"id":"root","text":"根节点","isExpand":"true","childNodes":"[{id\u003droot, text\u003d根节点, showCheck\u003dtrue}]"}]

格式不符合,换一种,直接用json:
JSONObject json = new JSONObject();
        json.put("id", "root");
        json.put("text", "根节点");
        json.put("isExpand", "true");
        json.put("childNodes", "["+con2List().toString().replace("\"", "'")+"]");

private static JSONObject con2List() throws JSONException{
        JSONObject jo = new JSONObject();
        jo.put("id", "org1");
        jo.put("text", "软件");
        jo.put("showCheck", "true");
        return jo;
}

打印出来的结果是:
[json]: {"id":"root","text":"根节点","isExpand":"true","childNodes":"[{'id':'org1','text':'软件','showCheck':'true'}]"}

格式也不符合,childNodes对应的是:"[{value}]",多了双引号。

有人说这个是标准的json格式,但是我需要的就是开头那样的格式,json也好,string也好都可以。

求大神来解答。
  • 写回答

2条回答 默认 最新

  • danielinbiti 2016-01-21 04:11
    关注
      json.put("childNodes", "["+con2List().toString().replace("\"", "'")+"]");
        这里应该把con2List先转成JsonArray
        然后put该jsonarray
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码