我现在需要获得一个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也好都可以。
求大神来解答。