JavaScript Patterns 3.5 JSON
JSON: JavaScript Object Notation
{"name": "value", "some": [1, 2, 3]}
The only syntax difference between JSON and the object literal is that property names need to be wrapped in quotes to be valid JSON. In object literals the quotes are required only when the property names are not valid identifiers, for example, they have spaces {"first name": "Dave"}.
In JSON strings you cannot use functions or regular expression literals.
Working with JSON
JSON.parse()
use the JSON.parse()method, which is part of the language since ES5 and is natively provided by the JavaScript engines in modern browsers.
For older JavaScript engines, you can use the JSON.org library (http://www.json.org/json2.js) to gain access to the JSON object and its methods.
// an input JSON string
var jstr = '{"mykey": "my value"}';
// antipattern
var data = eval('(' + jstr + ')');
// preferred
var data = JSON.parse(jstr);
console.log(data.mykey); // "my value"
using YUI3
// an input JSON string
var jstr = '{"mykey": "my value"}';
// parse the string and turn it into an object
// using a YUI instance
YUI().use('json-parse', function (Y) {
var data = Y.JSON.parse(jstr);
console.log(data.mykey); // "my value"
});
using JQuery
// an input JSON string
var jstr = '{"mykey": "my value"}';
var data = jQuery.parseJSON(jstr);
console.log(data.mykey); // "my value"
JSON.stringify()
var dog = {
name: "Fido",
dob: new Date(),
legs: [1, 2, 3, 4]
};
var jsonstr = JSON.stringify(dog);
// jsonstr is now:
// {"name":"Fido","dob":"2010-04-11T22:36:22.436Z","legs":[1,2,3,4]}
JavaScript Patterns 3.5 JSON的更多相关文章
- JavaScript Patterns 4.8 Function Properties - A Memoization Pattern
Gets a length property containing the number of arguments the function expects: function func(a, b, ...
- [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")
javascript eval函数解析json数据时为什么 加上圆括号?为什么要 eval这里要添加 “("("+data+")");//”呢? 原因在于: ...
- JavaScript标准库之——JSON
JSON 对象包含了两个方法,一是解析 JavaScript Object Notation (JSON),二是将值转换为 JSON.这个对象本身不能被调用或者作为构造函数,除了它的这两个方法属性外 ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
随机推荐
- IOS开发UI基础UISegment属性
UISegment属性 1.segmentedControlStyle设置segment的显示样式.typedef NS_ENUM(NSInteger, UISegmentedControlStyle ...
- iOS 9.2新增API
CloudKit 新增CKFetchWebAuthTokenOperation类 CKFetchWebAuthTokenOperation对象从使用指定的cloudkit中的APIToken获取一个w ...
- 三、Authentication & sessionid
客户在访问Django的某些敏感资料时,被要求需要先登录,客户通过/admin/login进行登录,客户登录成功后,Django给客户分配一个sessionid,后续的访问过程,客户端只需在http头 ...
- Web性能
使用Web性能测试可以很容易地创建一组可重复的测试,从而帮助我们分析web应用程序的性能,找到性能瓶颈. Web性能测试可以验证一个Web应用程序的行为是否正确.它们会向目标Web应用程序发布一组有序 ...
- 网狐6603棋牌游戏源码.rar
网狐6603棋牌游戏源码.rar http://pan.baidu.com/s/1dFgGNq5 网络收集仅供学习,下载后请务必在24小时内删除! 以上是原vc6.0源码,下载后使用vs2015编译, ...
- IIS启动网站出错的几个解决方法
在ASP.NET项目中使用了IIS服务器,由于系统是XP的,而在装系统的时候IIS没有一起装,所以从网上下载的IIS5.0版本(其它版本XP是用不了的).但是在使用的过程中老是出问题,每次调试好后,过 ...
- 用C#开发的双色球走势图(一)
首先声明,个人纯粹无聊之作,不作商业用途. 我相信每个人都拥有一个梦想那就是有朝一日能中500W,这个也一直是我的梦想,并默默每一期双色球或多或少要贡献自己一点点力量,本人并不属于那种铁杆的彩票迷,每 ...
- R语言-简单线性回归图-方法
目标:利用R语言统计描绘50组实验对比结果 第一步:导入.csv文件 X <- read.table("D:abc11.csv",header = TRUE, sep = & ...
- sql server2008中sql server身份能登录,window身份登录不了
用sql server身份的sa登录成功进入,一切正常,用window身份登录不了,问题如下
- 在博客中使用MathJax写数学公式
前言 总结一些在博客园使用MathJax写数学公式的经验. 博客园 设置使用数学公式 进入你的博客:管理 > 选项 里面有个启用数学公式支持,选上后保存. 这时,你就可以在你的博客里写数学公式了 ...