JSON

简介:

JSON(JavaScript Object Notation)(RFC 4627)是目前Web数据交换的事实标准。本文记叙JSON的格式,以及Javascript与Python的JSON库使用实践。

语法

JSON语法可以表述以下三种类型的值:

  • 简单值 使用JavaScript相同的语法,可以表示字符串、数值、布尔值、null、(没有undefined)。例如5,"Hello World",null,都属于简单值。
  • 对象 复杂数据类型,一组无序的键值对,每个键值对的键必须是双引号括起的字符串,值可以是简单值也可以是复杂数据类型。例如
     
      
    1. {
    2. "name": "haha"
    3. "age": 74
    4. }
  • 数组 复杂数据类型,表示有序的值的列表,可通过数值索引访问,数组的值可以是简单值也可以是复杂数据类型,且同一数组内可以同时出现任意类型的值。例如
     
      
    1. ["a", 2, {"b":3},[4, 5, 6] ]

JSON对象中的键应当是独一无二的,但在JSON表示中没有强制这一点,通常的做法都是取最后一次出现的value作为重复key的值。

编码

RFC规定了JSON必须使用UTF-8,UTF-16,UTF-32中的编码来表示。一般使用UTF-8编码。在JSON的字符串中最好不要使用unicode字面值,使用\uxxxx的形式表示能有效降低乱码的可能性。

RFC禁止JSON的字符表示前出现BOM标记。

RFC没有明确禁止JSON的string中出现非法Unicode字节序列,比如“unpaired UTF-16 surrogates”。

解析与序列化(Javascript)

早期的JSON解析器基本上就是使用eval()函数进行的。
但这样做存在安全隐患,所以在浏览器或者Node中定义有全局模块JSON

序列化: JSON.stringify

JSON.stringify(value[, replacer[, space]])

JSON.stringify方法会忽略所有原型成员,以及所有值为undefined的属性。
该方法可以指定两个可选参数:过滤器函数与缩进字符。

过滤器函数
  • If you return a Number, the string corresponding to that number is used as the value for the property when added to the JSON string.
  • If you return a String, that string is used as the property’s value when adding it to the JSON string.
  • If you return a Boolean, “true” or “false” is used as the property’s value, as appropriate, when adding it to the JSON string.
  • If you return any other object, the object is recursively stringified into the JSON string, calling the replacer function on each property, unless the object is a function, in which case nothing is added to the JSON string.
  • If you return undefined, the property is not included in the output JSON string.
 
  1. function replacer(key, value) {
  2. if (typeof value === "string") {
  3. return undefined;
  4. }
  5. return value;
  6. }
  7. var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
  8. var jsonString = JSON.stringify(foo, replacer);

可以在过滤器函数中滤除不必要的属性

space参数

如果指定为数字,使用空格作为indent,最大为10.
也可以指定为其他字符。

 
  1. JSON.stringify({ uno: 1, dos: 2 }, null, '\t');
  2. // returns the string:
  3. // '{
  4. // "uno": 1,
  5. // "dos": 2
  6. // }'

toJSON

JSON.stringify的解析顺序如下

  • 如果存在toJSON方法且能通过该方法取得有效值,调用该方法,否则返回对象本身。
  • 如果提供第二个参数:函数过滤器,使用它,传入第一步的返回值。
  • 对第二步返回的每个值进行相应的序列化。
  • 执行格式化。
 
  1. var obj = {
  2. foo: 'foo',
  3. toJSON: function() {
  4. return 'bar';
  5. }
  6. };
  7. JSON.stringify(obj); // '"bar"'
  8. JSON.stringify({ x: obj }); // '{"x":"bar"}'

解析JSON.parse()

JSON.parse(text[, reviver])
从字符串中解析JSON,可以选定一个reviver函数作为Hooki。

 
  1. JSON.parse('{}'); // {}
  2. JSON.parse('true'); // true
  3. JSON.parse('"foo"'); // "foo"
  4. JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
  5. JSON.parse('null'); // null
Reviver函数
 
  1. JSON.parse('{"p": 5}', function(k, v) {
  2. if (typeof v === 'number') {
  3. return v * 2; // return v * 2 for numbers
  4. }
  5. return v; // return everything else unchanged
  6. });

解析与序列化(Python)

python的JSON模块基本与Js类似。

类型映射

JSON PYTHON
JSON Python
object dict
array list
string unicode
number (int) int, long
numbe (real) float
true True
false False
null None

JSON序列化

 
  1. >>> import json
  2. >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
  3. '["foo", {"bar": ["baz", null, 1.0, 2]}]'
  4. >>> print json.dumps("\"foo\bar")
  5. "\"foo\bar"
  6. >>> print json.dumps(u'\u1234')
  7. "\u1234"
  8. >>> print json.dumps('\\')
  9. "\\"
  10. >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
  11. {"a": 0, "b": 0, "c": 0}
  12. >>> print json.dumps(None)'
  13. null

其格式可以通过indent参数控制。

 
  1. >>> import json
  2. >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
  3. '[1,2,3,{"4":5,"6":7}]'
  4. >>> import json
  5. >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True,
  6. ... indent=4, separators=(',', ': '))
  7. {
  8. "4": 5,
  9. "6": 7
  10. }

JSON解析

 
  1. >>> import json
  2. >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
  3. [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
  4. >>> json.loads('"\\"foo\\bar"')
  5. u'"foo\x08ar'

JSON格式化工具 (Shell)

 
  1. $ echo '{"json":"obj"}' | python -mjson.tool
  2. {
  3. "json": "obj"
  4. }
目录
相关文章
|
3月前
|
JSON 前端开发 JavaScript
JSON 必知必会
JSON 必知必会
N..
|
1月前
|
存储 JSON 前端开发
JSON
JSON
N..
16 1
|
1月前
|
存储 JSON JavaScript
JSON应用
JSON应用
28 4
|
3月前
C++Json生成
C++Json生成
23 0
|
8月前
|
JSON Java 数据格式
处理json 和HttpMessageConverterT
处理json 和HttpMessageConverterT
52 0
|
4月前
|
JSON 前端开发 JavaScript
JSON小技巧
JSON小技巧
25 0
|
8月前
|
JSON fastjson Java
JSON小记
JSON小记
|
10月前
|
XML JSON JavaScript
json
json
50 0
|
10月前
|
JSON 数据格式
json的一些小理解
个人学习总结
59 0
|
存储 JSON JavaScript
JSON介绍
JSON介绍
171 0