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,html,json)