首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript中字符串转化作json对象

2012-10-24 
javascript中字符串转化为json对象http://www.json.org/js.html To convert a JSON text into an object,

javascript中字符串转化为json对象

http://www.json.org/js.html
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.

var myObject = eval('(' + myJSONtext + ')');

举例:
???? var str = '{host:"localhost",sourceDir:"d:\\temp"}';
??? var jsonstr = eval('('+str+')');
??? alert(jsonstr.host);//localhost

热点排行