json:如何将json-string转换为javascript对象

cla*_*amp 2 javascript xml json

我正在将我的ajax代码从xml转换为json,但我遗漏了一些基本的东西:

当我在客户端收到json-string时,建议将它转换为javascript对象的方法是什么.

例如,我得到这个字符串:

{"connectionid":12345}
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

alert(xmlhttp.responseText.connectionid);
Run Code Online (Sandbox Code Playgroud)

谢谢!

ale*_*lex 8

使用JSON.parse(),或者eval(),如果您喜欢危险地生活(或完全信任您的JSON来自哪里).

如果你碰巧使用jQuery,你会得到$.parseJSON().