JSON字符串转成对象

有时候get或POST返回的是一个JSON格式的字符串,像:
'{"result":true,"count":1}'

这时候,我们要读取里面的result或count,就需要把字符串转成对象: 转换方法:

1,

var json = '{"result":true,"count":1}';
obj = JSON && JSON.parse(json) || $.parseJSON(json);
alert(obj.result);
alert(obj.count);

2.

var strJSON = '{"result":true,"count":1}';
var objJSON = eval("(function(){return " + strJSON + ";})()");
alert(objJSON.result);
alert(objJSON.count);

来自:http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript

你可能感兴趣的:(json字符串)