JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。

JSON 基础

JSON 的基础结构有两种:键值对 (name/value pairs) 和数组 (array)。

JSON 具有以下形式:

  • object - 对象,用花括号表示,形式为(数据是无序的):
    • { pair_1, pair_2, ..., pair_n }
  • pair - 键值对,形式为:
    • string : value
  • array - 数组,用中括号表示,形式为(数据是有序的):
    • [value_1, value_2, ..., value_n ]
  • value - 值,可以是
    • string 字符串
    • number 数字
    • object 对象
    • array 数组
    • true / false / null 特殊值
  • string 字符串

例子:

{
    "name": "echo",
    "age": 24,
    "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"],
    "ages for school": { 
        "primary school": 6,
        "middle school": 9,
        "high school": 15,
        "university": 18
    },
    "hobby": ["sports", "reading"],
    "married": false
}

JSON 与 Python 的转换

假设我们已经将上面这个 JSON 对象写入了一个字符串:

import json
from pprint import pprint

info_string = """
{
 "name": "echo",
 "age": 24,
 "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"],
 "ages for school": { 
 "primary school": 6,
 "middle school": 9,
 "high school": 15,
 "university": 18
 },
 "hobby": ["sports", "reading"],
 "married": false
}
"""

我们可以用 json.loads() (load string) 方法从字符串中读取 JSON 数据:

info = json.loads(info_string)

pprint(info)
{u'age': 24,
 u'ages for school': {u'high school': 15,
                      u'middle school': 9,
                      u'primary school': 6,
                      u'university': 18},
 u'coding skills': [u'python',
                    u'matlab',
                    u'java',
                    u'c',
                    u'c++',
                    u'ruby',
                    u'scala'],
 u'hobby': [u'sports', u'reading'],
 u'married': False,
 u'name': u'echo'}、

此时,我们将原来的 JSON 数据变成了一个 Python 对象,在我们的例子中这个对象是个字典(也可能是别的类型,比如列表):

type(info)
dict

可以使用 json.dumps() 将一个 Python 对象变成 JSON 对象:

info_json = json.dumps(info)

print info_json
{"name": "echo", "age": 24, "married": false, "ages for school": {"middle school": 9, "university": 18, "high school": 15, "primary school": 6}, "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"], "hobby": ["sports", "reading"]}

从中我们可以看到,生成的 JSON 字符串中,数组的元素顺序是不变的(始终是 ["python", "matlab", "java", "c", "c++", "ruby", "scala"]),而对象的元素顺序是不确定的。

生成和读取 JSON 文件

与 pickle 类似,我们可以直接从文件中读取 JSON 数据,也可以将对象保存为 JSON 格式。

  • json.dump(obj, file) 将对象保存为 JSON 格式的文件
  • json.load(file) 从 JSON 文件中读取数据
with open("info.json", "w") as f:
    json.dump(info, f)

可以查看 info.json 的内容:

with open("info.json") as f:
    print f.read()
{"name": "echo", "age": 24, "married": false, "ages for school": {"middle school": 9, "university": 18, "high school": 15, "primary school": 6}, "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"], "hobby": ["sports", "reading"]}

从文件中读取数据:

with open("info.json") as f:
    info_from_file = json.load(f)

pprint(info_from_file)
{u'age': 24,
 u'ages for school': {u'high school': 15,
                      u'middle school': 9,
                      u'primary school': 6,
                      u'university': 18},
 u'coding skills': [u'python',
                    u'matlab',
                    u'java',
                    u'c',
                    u'c++',
                    u'ruby',
                    u'scala'],
 u'hobby': [u'sports', u'reading'],
 u'married': False,
 u'name': u'echo'}

删除生成的文件:

import os
os.remove("info.json")
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 1 天前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐