当前位置:文档之家› 不同json数据格式之间的转换

不同json数据格式之间的转换

private static void toEasyuiTreeJson(JSONObject jsonObject, JSONObject target,int i) {
JSONArray array = null;
target.put("id", i);
if (jsonObject.get("button")!=null){
待转换的json数据格式为:
[{
"id":1,
"text":"菜单",
"children":[{
"id":11,
"text":"菜单1",
"state":"closed",
"children":[{
"id":111,
"text":"测试11",
"attributes":{
"key":"Custom Attribute1"
Iterator itr=array.iterator();
while(itr.hasNext()){
JSONObject jObject=new JSONObject();
list.add(jObject);
toEasyuiTreeJson((JSONObject)itr.next(), jObject, ++i);
target.put("type", "view");
target.put("url", url);
}
if (key != null) {
target.put("type", "click");
target.put("key", key);
}
}
}
JSONArray jsonArray = (JSONArray) jsonObject.get("children");
"sub_button": [ ]
},
{
"type": "click",
"name": "测试22",
"key": "Custom Attribute1",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试23",
"key": "Custom Attribute1",
}
if("view".equals(type)){
json.accumulate("url", jsonObject.getString("url"));
}
target.put("attributes", json);
}
if(array!=null){
List<JSONObject> list=new ArrayList<JSONObject>();
"name": "测试13",
"key": "Custom Attribute1",
"sub_button": [ ]
}
]
},
{
"name": "菜单2",
"sub_button": [
{
"type": "click",
"name": " "Custom Attribute1",
target.element("button", ja0);
} else {
target.element("sub_button", ja0);
}
}
}
该函数用了递归的思想。
反过来,先要把微信自定义菜单的json数据格式转换成Query easyui tree的json数据格式,可用如下函数,该函数同样用了递归的思想。
"attributes":{
"key":"Custom Attribute1"
}
}]
}]
}]
该json格式为jQuery easyui tree的json数据格式,先要把它转换成微信自定义菜单的json数据格式,如:
{
"button": [
{
"name": "菜单1",
"sub_button": [
String key = null;
if (json.containsKey("url")) {
url = json.getString("url");
}
if (json.containsKey("key")) {
key = json.getString("key");
}
if (url != null) {
}
JSONArray array2=new JSONArray();
array2.addAll(list);
target.element("children",array2);
}
}
"id":121,
"text":"测试21",
"attributes":{
"key":"Custom Attribute1"
}
},{
"id":122,
"text":"测试22",
"attributes":{
"key":"Custom Attribute1"
}
},{
"id":123,
"text":"测试23",
}
if(jsonObject.containsKey("type")){
String type=jsonObject.getString("type");
JSONObject json=new JSONObject();
if("click".equals(type)){
json.accumulate("key", jsonObject.getString("key"));
target.put("text", "菜单");
array=jsonObject.getJSONArray("button");
}else{
target.put("text", jsonObject.get("name"));
array=jsonObject.getJSONArray("sub_button");
list.add(jo2);
toWeixinMenuJson((JSONObject) itr.next(), jo2);
}
JSONArray ja0 = new JSONArray();
ja0.addAll(list);
if (Integer.parseInt(jsonObject.getString("id").toString()) == 1) {
"sub_button": [ ]
}
]
}
]
}
所用函数为:
private static void toWeixinMenuJson(JSONObject jsonObject, JSONObject target) {
if (Integer.parseInt(jsonObject.getString("id").toString()) != 1) {
if (jsonArray != null) {
List<JSONObject> list = new ArrayList<JSONObject>();
Iterator itr = jsonArray.iterator();
while (itr.hasNext()) {
JSONObject jo2 = new JSONObject();
}
},{
"id":112,
"text":"测试12",
"attributes":{
"key":"xx"
},
},{
"id":113,
"text":"测试13",
"attributes":{
"key":"Custom Attribute1"
}
相关主题