SoFunction
Updated on 2025-04-12

JSON serialization and parsing native JS methods and IE6 and chrome tests pass


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Nancle from CAU CS 101" />
<title>JSON serialization and parsing (native JS + Object object expansion method) [IE6 and chrome test passed]</title>
</head>
<script type="text/javascript">
var ele = {
x:11,
y:'string',
z:{x:11, y:'string'}
}
toJSON = function(obj){
var arr = [];
for(var key in obj){
var value = obj[key];
if(value == null){
value = '';
}else{
value = (typeof value === 'string' | typeof value === 'number')
? ('"' + value + '"') : toJSON(value);
}
var str = '"' + key + '":' + value;
(str);
}
return '{' + (',') + '}';
}
var str = toJSON(ele);
alert('The string replaced is: ' + str );
var ele2 = eval('(' + str + ')');
alert('Paste the string to get the js object: x=' + + ',y=' + + ',z=' + );
</script>
<body>
</body>
</html>