JSON-js - JSON in JavaScript.
(JSON-js - JavaScript中的JSON。)
To convert an object to a string, use JSON.stringify
:
(要将对象转换为字符串,请使用JSON.stringify
:)
var json_text = JSON.stringify(your_object, null, 2);
To convert a JSON string to object, use JSON.parse
:
(要将JSON字符串转换为对象,请使用JSON.parse
:)
var your_object = JSON.parse(json_text);
It was recently recommended by John Resig :
(它最近由John Resig推荐:)
...PLEASE start migrating your JSON-using applications over to Crockford's json2.js.
(...请开始将使用JSON的应用程序迁移到Crockford的json2.js。)
It is fully compatible with the ECMAScript 5 specification and gracefully degrades if a native (faster!) implementation exists.(它与ECMAScript 5规范完全兼容,如果存在本机(更快!)实现,则会优雅地降级。)
In fact, I just landed a change in jQuery yesterday that utilizes the JSON.parse method if it exists, now that it has been completely specified.
(事实上,我刚刚在jQuery中进行了一次更改,它使用了JSON.parse方法(如果存在的话),现在已经完全指定了。)
I tend to trust what he says on JavaScript matters :)
(我倾向于相信他在JavaScript问题上所说的话:))
All modern browsers (and many older ones which aren't ancient) support the JSON object natively.
(所有现代浏览器 (以及许多不古老的旧浏览器 )本身都支持JSON对象 。)
The current version of Crockford's JSON library will only define JSON.stringify
and JSON.parse
if they're not already defined, leaving any browser native implementation intact.(Crockford的JSON库的当前版本将仅定义JSON.stringify
和JSON.parse
如果它们尚未定义),则保留任何浏览器本机实现。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…