待转换的json数据格式为:
[{
"id":1,
"text":"菜单",
"children":[{
"id":11,
"text":"菜单1",
"state":"closed",
"children":[{
"id":111,
"text":"测试11",
"attributes":{
"key":"Custom Attribute1" }
},{
"id":112,
"text":"测试12",
"attributes":{
"key":"xx"
},
},{
"id":113,
"text":"测试13",
"attributes":{
"key":"Custom Attribute1" }
}]
},{
"id":12,
"text":"菜单2",
"children":[{
"id":121,
"text":"测试21",
"attributes":{
"key":"Custom Attribute1" }
},{
"id":122,
"text":"测试22",
"attributes":{
"key":"Custom Attribute1" }
},{
"id":123,
"text":"测试23",
"attributes":{
"key":"Custom Attribute1"
}
}]
}]
}]
该json格式为jQuery easyui tree的json数据格式,先要把它转换成微信自定义菜单的json数据格式,如:
{
"button": [
{
"name": "菜单1",
"sub_button": [
{
"type": "click",
"name": "测试11",
"key": "Custom Attribute1",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试12",
"key": "xx",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试13",
"key": "Custom Attribute1",
"sub_button": [ ]
}
]
},
{
"name": "菜单2",
"sub_button": [
{
"type": "click",
"name": "测试21",
"key": "Custom Attribute1",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试22",
"key": "Custom Attribute1",
"sub_button": [ ]
},
{
"type": "click",
"name": "测试23",
"key": "Custom Attribute1",
"sub_button": [ ]
}
]
}
]
}
所用函数为:
private static void toWeixinMenuJson(JSONObject jsonObject, JSONObject target) { if (Integer.parseInt(jsonObject.getString("id").toString()) != 1) { target.put("name", jsonObject.getString("text"));
JSONObject json = null;
json = jsonObject.getJSONObject("attributes");
if (json.size() != 0) {
String url = null;
String key = null;
if (json.containsKey("url")) {
url = json.getString("url");
}
if (json.containsKey("key")) {
key = json.getString("key");
}
if (url != null) {
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"); if (jsonArray != null) {
List<JSONObject> list = new ArrayList<JSONObject>();
Iterator itr = jsonArray.iterator();
while (itr.hasNext()) {
JSONObject jo2 = new JSONObject();
list.add(jo2);
toWeixinMenuJson((JSONObject) itr.next(), jo2);
}
JSONArray ja0 = new JSONArray();
ja0.addAll(list);
if (Integer.parseInt(jsonObject.getString("id").toString()) == 1) {
target.element("button", ja0);
} else {
target.element("sub_button", ja0);
}
}
}
该函数用了递归的思想。
反过来,先要把微信自定义菜单的json数据格式转换成Query easyui tree的json数据格式,可用如下函数,该函数同样用了递归的思想。
private static void toEasyuiTreeJson(JSONObject jsonObject, JSONObject target,int i) { JSONArray array = null;
target.put("id", i);
if (jsonObject.get("button")!=null){
target.put("text", "菜单");
array=jsonObject.getJSONArray("button");
}else{
target.put("text", jsonObject.get("name"));
array=jsonObject.getJSONArray("sub_button");
}
if(jsonObject.containsKey("type")){
String type=jsonObject.getString("type");
JSONObject json=new JSONObject();
if("click".equals(type)){
json.accumulate("key", jsonObject.getString("key"));
}
if("view".equals(type)){
json.accumulate("url", jsonObject.getString("url"));
}
} target.put("attributes", json); } if(array!=null){ List<JSONObject> list=new ArrayList<JSONObject>(); Iterator itr=array.iterator(); while(itr.hasNext()){ JSONObject jObject=new JSONObject(); list.add(jObject); toEasyuiTreeJson((JSONObject)itr.next(), jObject, ++i); } JSONArray array2=new JSONArray(); array2.addAll(list); target.element("children",array2); }
第二篇:Json格式数据
JSON的定义、结构及使用示例
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。它基于ECMA262语言规范(1999-12第三版)中JavaScript编程语言的一个子集。 JSON采用与编程语言无关的文本格式,但是也使用了类C语言(包括C, C++, C#, Java, JavaScript, Perl, Python等)的习惯,这些特性使JSON成为理想的数据交换格式。JSON的结构基于下面两点
1. "名称/值"对的集合 不同语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),键列表(keyed list)等
2. 值的有序列表 多数语言中被理解为数组(array)
JSON使用:
JSON以一种特定的字符串形式来表示 JavaScript 对象。如果将具有这样一种形式的字符串赋给任意一个 JavaScript 变量,那么该变量会变成一个对象引用,而这个对象就是字符串所构建出来的,好像有点拗口,我们还是用实例来说明。
这里假设我们需要创建一个User对象,并具有以下属性
用户ID
用户名
用户Email
您可以使用以下JSON形式来表示User对象:
{"UserID":11, "Name":"tht", "Email":"18039010◎qq.com"};
然后如果把这一字符串赋予一个JavaScript变量,那么就可以直接使用对象的任一属性了。完整代码:
<script>var User = {"UserID":11, "Name":"tht", "Email":"18039010◎
qq.com"};alert(User.Name);</script>
实际使用时可能更复杂一点,比如我们为Name定义更详细的结构,使它具有FirstName和LastName:
{"UserID":11, "Name":{"FirstName":"tht","LastName":"Tang"},
"Email":"18039010@qq.com"}
完整代码:
<script>var User = {"UserID":11, "Name":{"FirstName":"tht","LastName":"Tang"}, "Email":"18039010@qq.com"};alert(User.Name.FirstName);</script>
现在我们增加一个新的需求,我们某个页面需要一个用户列表,而不仅仅是一个单一的用户信息,那么这里就需要创建一个用户列表数组。
Json数据格式如下:
{"dataSet":{
"header":{
"returnCode":"0",
"errorInfo":"HTTP请求错误",
"version":"V1.0R010",
这里以jquery异步获取的数据类型——json对象和字符串为依据,分别介绍两种方式获取到的结果处理方式。
1.对于服务器返回的JSON字符串,如果jquery异步请求没做类型说明,或者以字符串方式接受,那么需要做一次对象化处理,方式不是太麻烦,就是将该字符串放于eval()中执行一次。这种方式也适合以普通javascipt方式获取json对象,以下举例说明:
var dataObj=eval("("+data+")");//转换为json对象
alert(dataObj.root.length);//输出root的子对象数量
$.each(dataObj.root,fucntion(idx,item){
if(idx==0){
return true;
}
//输出每个root子对象的名称和值
alert("name:"+item.name+",value:"+item.value);
})
注:对于一般的js生成json对象,只需要将$.each()方法替换为for语句即可,其他不变。
2.对于服务器返回的JSON字符串,如果jquery异步请求将type(一般为这个配置属性)设为“json”,或者利用$.getJSON()方法获得服务器返回,那么就不需要eval()方法了,因为这时候得到的结果已经是json对象了,只需直接调用该对象即可,这里以$.getJSON方法为例说明数据处理方法:
$.getJSON("http://gaoyusi./",{param:"gaoyusi"},function(data){
//此处返回的data已经是json对象
//以下其他操作同第一种情况
$.each(data.root,function(idx,item){
if(idx==0){
return true;//同countinue,返回false同break
}
alert("name:"+item.name+",value:"+item.value);
});
});
这里特别需要注意的是方式1中的eval()方法是动态执行其中字符串(可能是js脚本)的,这样很容易会造成系统的安全问题。所以可以采用一些规避了eval()的第三方客户端脚本库,比如JSON in JavaScript就提供了一个不超过3k的脚本库。
JsonResult j = Json(new
{
Result = true,
FileName = new
FileInfo(Request.Files[0].FileName).Name,
FileUrl = fileurl.Replace("\"", "")
});
Response.write(j.Data.ToString());