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 ...
随机推荐
- .NET VS2012 将代码同步上传到 oschina.net 和 github
1.先首要注册两个账号 https://github.com/ http://git.oschina.net/ 2.下载 getextendions http://sourceforge.net/pr ...
- [ASP.NET] 下一代ASP.NET开发规范:OWIN
今天投简历 准备面试了... 本节目录: OWIN简介 OWIN规范 Katana Hello World(3种Host) 自定义Middleware OWIN简介 OWIN(Open Web Int ...
- sprint3与总结
backlog-看板-燃尽图-每日立会 github:https://github.com/alfredzhu/team-work 总结:这种团队合作的方式很好,大家在一起沟通,相互交流想法,一起解决 ...
- PowerDesigner的安装和数据库创建(转载)
此文描述详细,特此转载,仅复制了大部分内容,可参考原文CodeSmith和PowerDesigner的安装和数据库创建(原创) 请大家不要用于商业用途哈,要支持正版,大家都是做软件的,知道开发一套软件 ...
- 使用RazorEngine对ASP.NET MVC的Views进行UnitTest
有的时候我们需要对Razor最后生产的文本(HTML OR XML OR..)进行单元测试. 使用Nuget安装RazorEngine. 新建一个ASP.NET MVC项目,并且带有测试项目. 修改I ...
- Lucene-Analyzer
Lucene文本解析器实现 把一段文本信息拆分成多个分词,我们都知道搜索引擎是通过分词检索的,文本解析器的好坏直接决定了搜索的精度和搜索的速度. 1.简单的Demo private static fi ...
- 【EF 译文系列】模型和数据库连接
原文链接:Connections and Models 本篇文章主要包括 Entity Framework 是如何选择数据库进行连接,以及我们如何去改变它的连接.无论是通过 Code First 还 ...
- 收集数据至泛型Dictionary
开发时,我们经常使用到泛型,不管是List<T>,还是Dictionary<T,V>,下面Insus.NET在测试一些功能,当使用到一些临时数据集时,有好几种方法把数据收集至D ...
- 分享一下学习css,js心得
简化代码,使页面简洁! web前端开发——将界面更好呈现给用户! 要了解在不同浏览器上的兼容情况.渲染原理和存在的bug! 网站性能优化.SEO: 代码的可维护性.性能: 网站重构的本质:建立一个前端 ...
- WPF 竖排文字
---恢复内容开始--- 想做一个WPF 文字竖排 类似上图.用在TabItem的header上面. <TextBlock FontSize="30" Text=" ...