json字符串转换
/*json转成字符串*/$.jsonToStr = function(obj){
var THIS = this;
switch (typeof(obj))
{
case 'string':
return '"'+ obj.replace(/(["\\])/g, '\\$1') + '"';
case 'array':
return '[' + obj.map(THIS.jsonToStr).join(',') + ']';
case 'object':
if (obj instanceof Array)
{
var strArr = [];
var len = obj.length;
for (var i = 0; i < len; i++)
{
strArr.push(THIS.jsonToStr(obj[i]));
}
return '[' + strArr.join(',') + ']';
}
else if(obj==null)
{
return 'null';
}
else
{
var string = [];
for (var property in obj) string.push(THIS.jsonToStr(property) + ':' + THIS.jsonToStr(obj[property]));
return '{'+ string.join(',') + '}';
}
case 'number':
return '"' + obj + '"';
case false:
return '"' + obj + '"';
}
}
/* 字符串转换为 json 对象 */
function str2json(str)
{
eval("var json_obj = " + str);
return json_obj;
}