当前位置: 代码迷 >> JavaScript >> javascript中字符串转化作json对象
  详细解决方案

javascript中字符串转化作json对象

热度:228   发布时间:2012-10-24 14:15:58.0
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