JSON是Spry可以接受的数据格式之一. 本文将讲述如何在在Spry中使用JSON,并提供一些技巧和帮助。
什么是JSON?
JSON 使用JavaScript对象标记。它是一个轻量的、灵活的、精确的数据存储与传输的方法。
基本的JSON代码如下:
{
"firstName": "Jason",
"lastName": "Jones",
"address": {
"city": "San Francisco, CA",
"zip": 94121,
"address": "444 Columbus Ave"
},
"email": [
"jason@sf.com",
"sjones@adobe.com"
]
}
"firstName": "Jason",
"lastName": "Jones",
"address": {
"city": "San Francisco, CA",
"zip": 94121,
"address": "444 Columbus Ave"
},
"email": [
"jason@sf.com",
"sjones@adobe.com"
]
}
让我们分析一下,JavaScript对象是一个名字/值对,基本结构如下:
{name:value,name:value,name:value}
数据类型
JSON 支持下列通用数据类型。
Boolean(布尔):
在JavaScript "true"和"false"是预先定义的关键字。
true 书写如下:
"active":true
false书写如下:
"active":false
String(字符串):
在JSON中,字符串必须被包含在双引号中。关于字符的规定请参考 JSON官方站点。
"address": "444 Columbus Ave"
Number(数值):
JSON支持的数值包括正整数:
"zip": 94121
负整数:
total points": -123
浮点数:
"length":122.2344
科学表示法:
"atoms per mole":-6.023e+23
Array(数组):
数组被包含在方括号中, 每一项用逗号分割, 数组可以包含JSON支持的所有结构。
"values":[ 1, false, "test", [ 2, 3 ], { "test": 1 } ]
在上面的范例中,数组包含了5个元素:数值、布尔、数组、对象。
Object(对象):
对象被包含在大括号中。对象包括0个或多个属性/值对,之间用逗号分割:
{ "count": 2, "results": [ 2, 3 ] }
在上面的范例中,对象包含 "count"和 "results"两个属性。"count"属性有一个整数值2, "results"是一个数组。
Null(空值):
在JavaScript中, null 是一个预先定义的对象,null书写如下:
"email":null
更多参考