前台解决Json数据中出现"$ref"的问题
2016-12-10 11:51
393 查看
当我们使用fastjson的时候,这个组件"很聪明",他会自动判断对象或者对象中是否有互相调用的死循环,从而用一些特殊的符号代替,这里就给出在js中解决问题的办法,小金子也是别的地方拿来的,也是通过混淆的,但是这不影响使用.
只要在你使用之前调用一下这句话
FastJson.format(data); //data是后台返回的json数据,里面有$ref字样的字符串,这是前面所说的fastjson处理的,但是通过这句话都能进行替换,把我们需要的数据还原回来
//页面解决json中$ref问题
var FastJson = {
isArray : function(a) {
return "object" == typeof a
&& "[object array]" == Object.prototype.toString.call(a)
.toLowerCase();
},
isObject : function(a) {
return "object" == typeof a
&& "[object object]" == Object.prototype.toString.call(a)
.toLowerCase();
},
format : function(a) {
if (null == a)
return null;
"string" == typeof a && (a = eval("(" + a + ")"));
return this._format(a, a, null, null, null);
},
_randomId : function() {
return "randomId_" + parseInt(1E9 * Math.random());
},
_getJsonValue : function(a, c) {
var d = this._randomId(), b;
b = "" + ("function " + d + "(root){") + ("return root." + c + ";");
b += "}";
b += "";
var e = document.createElement("script");
e.id = d;
e.text = b;
document.body.appendChild(e);
d = window[d](a);
e.parentNode.removeChild(e);
return d;
},
_format : function(a, c, d, b, e) {
d || (d = "");
if (this.isObject(c)) {
if (c.$ref) {
var g = c.$ref;
0 == g.indexOf("$.")
&& (b[e] = this._getJsonValue(a, g.substring(2)));
return
}
for ( var f in c)
b = d, "" != b && (b += "."), g = c[f], b += f, this
._format(a, g, b, c, f);
} else if (this.isArray(c))
for (f in c)
b = d, g = c[f], b = b + "[" + f + "]", this._format(a, g,
b, c, f);
return a;
}
};
只要在你使用之前调用一下这句话
FastJson.format(data); //data是后台返回的json数据,里面有$ref字样的字符串,这是前面所说的fastjson处理的,但是通过这句话都能进行替换,把我们需要的数据还原回来
//页面解决json中$ref问题
var FastJson = {
isArray : function(a) {
return "object" == typeof a
&& "[object array]" == Object.prototype.toString.call(a)
.toLowerCase();
},
isObject : function(a) {
return "object" == typeof a
&& "[object object]" == Object.prototype.toString.call(a)
.toLowerCase();
},
format : function(a) {
if (null == a)
return null;
"string" == typeof a && (a = eval("(" + a + ")"));
return this._format(a, a, null, null, null);
},
_randomId : function() {
return "randomId_" + parseInt(1E9 * Math.random());
},
_getJsonValue : function(a, c) {
var d = this._randomId(), b;
b = "" + ("function " + d + "(root){") + ("return root." + c + ";");
b += "}";
b += "";
var e = document.createElement("script");
e.id = d;
e.text = b;
document.body.appendChild(e);
d = window[d](a);
e.parentNode.removeChild(e);
return d;
},
_format : function(a, c, d, b, e) {
d || (d = "");
if (this.isObject(c)) {
if (c.$ref) {
var g = c.$ref;
0 == g.indexOf("$.")
&& (b[e] = this._getJsonValue(a, g.substring(2)));
return
}
for ( var f in c)
b = d, "" != b && (b += "."), g = c[f], b += f, this
._format(a, g, b, c, f);
} else if (this.isArray(c))
for (f in c)
b = d, g = c[f], b = b + "[" + f + "]", this._format(a, g,
b, c, f);
return a;
}
};
相关文章推荐
- 通过ajax读取json格式数据字符串出现回车时出错问题解决
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- 如何解决数据保存数据库后出现中文变"?"的问题
- 解决content-type为"application/json"的post过来的数据在php端接受不到的问题
- 用ajax传递json到前台中文出现问号乱码问题的解决办法
- 解决springmvc返回json数据IE出现文件下载和json数据中文乱码问题
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- Django学习问题——解决返回JSON数据时中文出现乱码
- SSM框架:解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码
- var dataObj=eval("("+data+")");//转换为json对象(解决在ajax返回json格式数据的时候明明正确的获取了返回值但是却就是进不去success方法的问题。格式错误)
- php 解决 json_encode 数据进入数据库 把"\" 去掉的问题
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- springmvc通过json向前台传递信息的时候出现乱码问题解决方法
- 关于java中long类型的数据转换json传到前台时丢失精度问题的解决办法
- 解决使用fastJson中出现$ref问题
- jQuery UI Autocomplete 使用 ajax 方法传输Json数据出现乱码问题的解决
- SpringMvc从后台返回到前台的json数据出现了乱码的问题
- 在listView中显示json数据出现的问题及解决方法