jQ实现JSON.stringify(obj)方法
jQstringify是使用jQuery实现的JSON.stringify(obj)方法
代码如下:
<script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery/jquery-1.10.2.min.js"></script>
function jQstringify( obj ) {
var arr = [];
$.each( obj, function( key, val ) {
var next = key + ": ";
next += $.isPlainObject( val ) ? printObj( val ) : val;
arr.push( next );
});
return "{ " + arr.join( ", " ) + " }";
};
var student = new Object();
student.name = "柯乐义";
student.age = "25";
student.location = "广州";
var json = JSON.stringify(student);
document.writeln('JSON.stringify(student)结果:');
document.writeln(json);
var myStringify=jQstringify(student)
document.writeln('jQstringify(student)结果:');
document.writeln(myStringify)
执行结果如下:
JSON.stringify(student)结果: {"name":"柯乐义","age":"25","location":"广州"} jQstringify(student)结果: { name: 柯乐义, age: 25, location: 广州 }
结果是一样的