Dojo库常用资源整理——dojo/json
来源:互联网 发布:数据挖掘和gis的关系 编辑:程序博客网 时间:2024/04/28 21:00
dojo/json
dojo/json is a module for JSON parsing and serialization based on the standard EcmaScript 5’s JSON object.
The module provides two functions, parse() andstringify().
Usage
parse()
The parse() function has a single required argument, the JSON string to be parsed, and an optional second argumentindicating if secure parsing should always be used. For example:
require(["dojo/json"], function(JSON){ JSON.parse('{"hello":"world"}', true);});
If the target platform supports native JSON parsing, dojo/json will always use the native parser (and serializer).
If no native parsing is available, dojo/json will useeval() to parse the JSON. Wheneval() is used, theparse() function’s second argument indicates if secure parsing should be used. Secure parsing will verify that theJSON is safe before evaluating it. Performing this verification is slower than directly evaluating, and secure parsingshould only be used if the JSON is from an unsecure source. The second argument has no effect on browsers with nativeparsing, since native parsing is always secure.
stringify()
The stringify() function takes a JavaScript value and serializes it to JSON. For example, to serialize an object wecould write:
require(["dojo/json"], function(JSON){ var jsonString = JSON.stringify({ hello: "world" });});
The stringify() function takes the same arguments as the standardJSON.stringify() function. This is explainedin more detail here:JSON.stringify()
The second argument is a replacer function, and the third argument allows you to provide a spacer string for prettyformatting.
Examples
Converts a JSON string into a JavaScript object, modifies it and then outputs the JSON string again.
require(["dojo/json", "dojo/dom", "dojo/on", "dojo/domReady!"],function(JSON, dom, on){ on(dom.byId("convert"), "click", function(){ var origin = '{"hello": "world"}'; var obj = JSON.parse(origin); obj.hello = "Dojo"; obj.something = "New"; var output = JSON.stringify(obj); dom.byId("output").innerHTML = output; });});
<button type="button" id="convert">Convert!</button><p><strong>Original JSON</strong></p><pre>{"hello": "world"}</pre><p><strong>Converted JSON</strong></p><pre id="output"></pre>
See Also
- dojo/string - String handling enhancements
- dojo/_base/array - Array handling enhancements
- Dojo库常用资源整理——dojo/json
- DOJO 资源
- dojo资源
- Java+JQuery+Dojo+Json学习资源
- Dojo学习笔记——Dojo事件
- dojo
- dojo
- Dojo
- dojo
- dojo
- dojo
- dojo相关网上资源
- Dojo 学习资源 url
- dojo 资源收集
- DOJO学习资源汇总
- 探索dojo/json
- dojo/json传数据
- DOJO Quick Start——学习dojo 第一课
- hdu 2572 终曲(水题)
- webstorm快捷键大全
- Dijkstra迪科斯彻算法
- 快速幂+乘法逆元求等比数列的和并取模
- hd 2700 Parity
- Dojo库常用资源整理——dojo/json
- /吸血鬼/
- 从零开始学习OpenCL开发(二)一个最简单的示例与简单性能分析
- IOS 启动画面和图标设置(适配IOS7 and Xcode5)
- 重启uwsgi服务器
- HDU_2080 夹角有多大II
- Oracle to_char格式化函数
- java组合和聚合的区别和联系
- 批处理文件工具(java+shell命令实现)