var Convert = {
StringToJSON: function(str) {
var a;
eval('a=' + str + ';');
return a;
},
ToJSONString: function(obj) {
switch(typeof(obj))
{
case 'object':
var ret = [];
if (obj instanceof Array)
{
for (var i = 0, len = obj.length; i < len; i++)
{
ret.push(Convert.ToJSONString(obj[i]));
}
return '[' + ret.join(',') + ']';
}
else if (obj instanceof RegExp)
{
return obj.toString();
}
else
{
for (var a in obj)
{
ret.push(a + ':' + Convert.ToJSONString(obj[a]));
}
return '{' + ret.join(',') + '}';
}
case 'function':
return 'function() {}';
case 'number':
return obj.toString();
case 'string':
return "\"" + obj.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function(a) {return ("\n"==a)?"":"";}) + "\"";
case 'boolean':
return obj.toString();
default:
return obj.toString();
}
}
};
来源:
http://hi.baidu.com/wangyu1987530/item/38aadaddf634a34edcf9befa
JOSN对象转化为字符串,字符串转化为JSON对象
最新推荐文章于 2023-09-25 20:01:03 发布