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 ...
 
随机推荐
- IT人的自我导向型学习:学习的3个维度
			
看到大家对我的文章赞了不少,看来大家还比较喜欢看.园子里的一些朋友和我说:”终于又看到你要在园子里发原创文章了.几年前就受益匪浅,经过几年的成长分享来的东西肯定也是精品.“ 感谢大家对我的信任,如果你 ...
 - 哀悼我的第一次"创业"经历
			
下周考完最后一科,大学四年时光基本上算是落下帷幕,剩下的就只是整整毕业设计了.如果按照正常的节奏,这个学期应该能够搞完毕业设计的2/3,但无奈还在广州的一家公司里面实习,没有多少时间弄,得拖到3月了. ...
 - Hekaton是如何影响你数据库的目标恢复时间(RTO)的
			
这个周末我发现了SQL Server 2014里Hekaton的一个有趣副作用,很遗憾它会负面影响你数据库的目标恢复时间(Recovery Time Objective,RTO).你已知道,对于每个本 ...
 - sqlite3存储格式
			
本篇介绍sqlite3数据库文件的存储格式.通过阅读源读源代码可以知道sqlite的设计思想.一个sqlite数据库文件对应着一个数据库.sqlite将数据库文件划分大小一致的存储(以区分内存)页面, ...
 - DBSet Class(EF基础系列11)
			
Method Name Return Type Description Add Added entity type Adds the given entity to the cont ...
 - Unity烂笔头1-自定义INSPECTOR属性窗口节点项
			
1.添加输入框和标签 LevelScript: using UnityEngine; using System.Collections; public class LevelScript : Mono ...
 - 【循序渐进学Python】13.基本的文件I/O
			
文件I/O是Python中最重要的技术之一,在Python中对文件进行I/O操作是非常简单的. 1. 打开文件 使用 open 函数来打开文件,语法如下: open(name[, mode[, buf ...
 - jquery实现全选功能
			
主要是模拟一些网页中的表格实现全选功能. <form> 你爱好的运动是: <input type="checkbox" id="Check" ...
 - 根据IP地址获取地址所在城市帮助类(IPHelper)
			
很多类库都是需要在长时间的编写过程中进行积累的,进入软件编程行业已经是第五个年头了,从2011年写下第一行代码到现在不知道已经写了多少行代码了,时间也过得挺快的.最近事情比较多,也很少写博客了,最近项 ...
 - PHP实现文字水印图片
			
php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...