读书人

javascript中字符串转化作json对象

发布时间: 2012-10-24 14:15:58 作者: rapoo

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

读书人网 >JavaScript

热点推荐