json分享
JSON是什么?
JavaScript Object Notation (JSON) is a text format for the serialization of structured data. It is derived from the object literals of JavaScript
JSON是JavaScript Object Notation的简称,JSON易于访问且结构性强,用来存储信息(数据)。
JSON例子
和 XML 一样,JSON 也是基于纯文本的数据格式。由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,您可以用 JSON 传输一个简单的 String,Number,Boolean,也可以传输一个数组,或者一个复杂的 Object 对象。
1、Object
var person = {
"name" : "Lily",
"gender" : "female",
"age" : "24"
}
我们创建了一个对象,可以通过person访问。在这个对象中我们声明3组数据,这些数据使用“名称:值”对的集合,以逗号分隔。我们可以直接通过属性的名称访问,例如:
console.log("姓名:"+person.name+" 性别:"+person.gender+" 年龄:"+person.age);
2、Array
var articles = [{ "id" : 111, "article" : "html", "author":"Lily" }, { "id" : 222, "article" : "css", "author":"Jack" } ]
对于Array格式的JSON数据,我们可以通过索引来访问:
console.log(articles[0].id); //
console.log(articles[1].author); //Jack
其他JSON例子:
{
"africaLagos": [
{
"from": -377711769600000,
"to": -1588464816000,
"dst": false,
"offset": 816,
"name": "LMT"
},
{
"from": -1588464816000,
"to": 253402300799000,
"dst": false,
"offset": 3600,
"name": "WAT"
}
]
}
Facebook JSON File
{
"data": [
{
"id": "X999_Y999",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2010!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
},
{
"id": "X998_Y998",
"from": {
"name": "Peyton Manning", "id": "X18"
},
"message": "Where's my contract?",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
}
]
}
Colors JSON File
{
"colorsArray":[{
"colorName":"red",
"hexValue":"#f00"
},
{
"colorName":"green",
"hexValue":"#0f0"
},
{
"colorName":"blue",
"hexValue":"#00f"
},
{
"colorName":"cyan",
"hexValue":"#0ff"
},
{
"colorName":"magenta",
"hexValue":"#f0f"
},
{
"colorName":"yellow",
"hexValue":"#ff0"
},
{
"colorName":"black",
"hexValue":"#000"
}
]
}
iPhone Menu JSON
{
"menu": {
"header": "xProgress SVG Viewer",
"items": [
{
"id": "Open"
},
{
"id": "OpenNew",
"label": "Open New"
},
null,
{
"id": "ZoomIn",
"label": "Zoom In"
},
{
"id": "ZoomOut",
"label": "Zoom Out"
},
{
"id": "OriginalView",
"label": "Original View"
},
null,
{
"id": "Quality"
},
{
"id": "Pause"
},
{
"id": "Mute"
},
null,
{
"id": "Find",
"label": "Find..."
},
{
"id": "FindAgain",
"label": "Find Again"
},
{
"id": "Copy"
},
{
"id": "CopyAgain",
"label": "Copy Again"
},
{
"id": "CopySVG",
"label": "Copy SVG"
},
{
"id": "ViewSVG",
"label": "View SVG"
},
{
"id": "ViewSource",
"label": "View Source"
},
{
"id": "SaveAs",
"label": "Save As"
},
null,
{
"id": "Help"
},
{
"id": "About",
"label": "About xProgress CVG Viewer..."
}
]
}
}
Customer Form JSON
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
Products Database JSON File
{
"name":"Product",
"properties":
{
"id":
{
"type":"number",
"description":"Product identifier",
"required":true
},
"name":
{
"description":"Name of the product",
"type":"string",
"required":true
},
"price":
{
"type":"number",
"minimum":0,
"required":true
},
"tags":
{
"type":"array",
"items":
{
"type":"string"
}
}
}
}
- 空格(空格、tab、换行、新行或回车)对JSON没有影响。它也可以压缩,对数据没有影响。
- 对象字面名称必须小写(例如, null, false, true 等)。
- 把所有名称/值放在引号中。
- 在使用你的JSON之前,先验证是否是有效的。 http://www.jsonlint.com
- JSON默认Unicode编码为UTF-8(查看所有应用程序的媒体类型http://www.iana.org/assignments/media-types/application/index.html)
- JSON文本的MIME媒体类型为application /json
JSON解析
JSON.parse(): 将JSON字符串转换成对象或数组,若失败则报错。
var str = '{"name":"Lily","age":"18"}';
console.log(typeof str); // string var obj = JSON.parse(str);
console.log(typeof obj); //object
JSON.stringify(): 用于将一个对象解析成字符串。
var obj = {"name":"Lily","age":"18"};
console.log(typeof obj); // object var str= JSON.stringify(obj);
console.log(typeof str); //string
JSON转义字符
下面的字符需要转义:引号", 斜杠/,反斜杠\, 换行n,回车r, tab t
引号"
var obj = {"name":"\"\/\\\n\r\tLily","age":"23"};
console.log(obj.name);
//output: "/\ Lily
JSON工具
BE JSON JSON在线校验
SO JSON 在线JSON校验
JSON Parser Online 简单的字符串解析和JS Eval
JSONLint 非常流行的在线JSON校验
Online JSON Tree Viewer 加载JSON文件,展示数据的树结构
参考文章:http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/
http://www.sitepoint.com/basics-json-syntax-tips/
最新文章
- javascript技术难点(三)之this、new、apply和call详解
- 使用jedis操作redis
- ThinkPHP升级指导
- skype msnLite 静态路由
- [转载+整理]JVM性能调优----JVM架构
- jquery mobile listview列表属性(搜索自动填充检索效果)
- Zookeeper分享
- Tyvj P1016 包装问题 (DP)
- leetcode202(Floyd判圈算法(龟兔赛跑算法))
- Angular - - $q 承诺与延迟
- MySQL基准测试(benchmark)
- 解决 apache poi 转换 word(docx) 文件到 html 文件表格没边框的问题
- 【spring源码分析】IOC容器初始化(十二)
- Python基础:编码规范(4)
- Gym 101911F “Tickets”
- 利用LOCK机制来定位前缀劫持者
- RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序”
- POJ 1511 链式前向星+SPFA
- 【scala】占位符
- laravel获取checkbox值的小技巧