一、概述
JSON (JavaScript Object Notation) ,是一种轻量级的数据交换格式。它的使用范围很广,并成为 ECMA 标准,可以被使用在多种编程语言中,用于前后端之间的数据传输、存储和交换数据。可以说是“用更少的编码,有更快的处理速度”,所以深受广大程序员的喜爱。
JSON 格式的数据是由键值对组成的,其中键值对之间使用逗号分隔。在 JSON 中,键和值都必须使用双引号引起来。
一个简单的 JSON 对象例子:
{
"name": "Apifox",
"age": 2,
"city": "Guangzhou"
}
二、JSON 的含义和类型
JSON 用于 JavaScript,把任何 JavaScript 对象变成 JSON,即把这个对象序列化成一个 JSON 格式的字符串,然后通过网络传递给其他计算机。JSON 格式的字符串由双引号 “ ”
包裹,由键值对组成,键和值之间使用冒号 :
分隔,键值对之间使用逗号 ,
分隔。值可以是字符串、数字、布尔、null、数组或对象等类型。
在 JSON 中,常见的数据类型有:
- 字符串:JavaScript 中的
string
,用于表示文本信息,包含反斜杠\
转义。
{ "name": "Apifox" }
- 数字:JavaScript 中的
number
,用于表示数值信息,可以是整数或浮点数。
{
"id": 48,
"price": 17.95
}
- 布尔值:JavaScript 中的
boolean
,用于表示真假信息。布尔值并不包含在引号内,而是被视为字符串值。
{ "allowLogin": false }
- null:JavaScript的
null
,用于表示空值。
{ "address": null }
- 对象:JavaScript的
{ ... }
,表示方式用于表示有序的值的集合,每个值可以是任意类型。
{
"category" : {
"id" : 675442 ,
"name" : "小狐"
}
}
- 数组:JavaScript的
Array
,表示方式为[]
,用于表示无序的键值对集合,每个键值对都是由键名和对应的值组成。
{
"list" : [
{
"name" : "Lihua",
"age" : 22,
}
{
"name" : "Zhangsan",
"age" : 25
}
]
}
下面是一个完整的 JSON 数据示例:
{
"name": "Simon",
"age": 2,
"isMale": true,
"hobbies": ["reading", "learning", "traveling"],
"address": {
"city": "Guangzhou",
"postCode": "510000"
},
"email": null
}
三、JSON 数据的使用场景
JSON 的应用非常广泛。它被用于前后端之间的数据传输,也被用于存储和交换数据。在 Web 开发中,JSON 通常被用来获取来自服务器的数据。这些数据可以被使用在网页上,以便实现动态的交互和更新页面内容。同时,JSON 也被广泛应用于移动应用程序、桌面应用程序以及各种互联网服务中。
相比于其它数据格式,JSON 具有以下优势:
- 简洁易读:JSON 的语法比较简洁,易于阅读和编写。
- 解析速度快:JSON 的解析速度通常比其他数据格式更快。
- 兼容性好:JSON 和 JavaScript 的兼容性非常好,因为它们共享着相同的语法和数据结构。
四、Python JSON 数据解析
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。
如果你还不了解 JSON,可以先阅读我们的 JSON 教程。
Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:
- json.dumps(): 对数据进行编码。
- json.loads(): 对数据进行解码。
在 json 的编解码过程中,Python 的原始类型与 json 类型会相互转换,具体的转化对照如下:
4.1、Python 编码为 JSON 类型转换对应表:
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str | string |
int, float, int- & float-derived Enums | number |
True | true |
False | false |
None | null |
4.2、JSON 解码为 Python 类型转换对应表:
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
五、解析示例
一个labelme标注的json文件
{
"version": "4.0.0",
"flags": {},
"shapes": [
{
"label": "track",
"points": [
[
634.0,
203.25925925925924
],
[
604.0,
274.25925925925924
],
[
603.0,
339.25925925925924
],
[
622.0,
362.25925925925924
],
[
639.0,
362.25925925925924
],
[
649.0,
353.25925925925924
],
[
682.0,
382.25925925925924
],
[
733.0,
389.25925925925924
],
[
748.0,
363.25925925925924
],
[
827.0,
358.25925925925924
],
[
829.0,
249.25925925925924
],
[
800.0,
193.25925925925924
],
[
775.0,
184.25925925925924
],
[
740.0,
198.25925925925924
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
{
"label": "track",
"points": [
[
860.0,
190.0
],
[
997.0,
186.0
],
[
998.0,
305.0
],
[
924.0,
320.0
],
[
905.0,
352.0
],
[
877.0,
353.0
],
[
869.0,
245.0
],
[
879.0,
222.0
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
{
"label": "car",
"points": [
[
924.0,
321.0
],
[
905.0,
352.0
],
[
909.0,
388.0
],
[
936.0,
404.0
],
[
959.0,
411.0
],
[
966.0,
431.0
],
[
1000.0,
432.0
],
[
1000.0,
306.0
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
}
],
"imagePath": "00000100.jpg",
"imageData": null,
"imageHeight": 563,
"imageWidth": 1000
}
使用python解析如下:
import json
# 指定JSON文件的路径
file_path = '00000100.json'
# 打开并读取JSON文件
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# 打印读取的数据
print(data)
# 访问特定的数据
print(f"version: {data['version']}")
print(f"flags: {data['flags']}")
print(f"imagePath: {data['imagePath']}")
print(f"imageData: {data['imageData']}")
print(f"imageHeight: {data['imageHeight']}")
print(f"imageWidth: {data['imageWidth']}")
shapes = data['shapes']
print(f"shapes: {shapes}")
for shape in shapes:
label = shape['label']
points = shape['points']
shape_type = shape['shape_type']
print(f"Label: {label}")
print(f"Shape Type: {shape_type}")
print(f"Points: {points}")
for point in points:
print(f"Point: {point}")
打印结果:
{'version': '4.0.0', 'flags': {}, 'shapes': [{'label': 'track', 'points': [[634.0, 203.25925925925924], [604.0, 274.25925925925924], [603.0, 339.25925925925924], [622.0, 362.25925925925924], [639.0, 362.25925925925924], [649.0, 353.25925925925924], [682.0, 382.25925925925924], [733.0, 389.25925925925924], [748.0, 363.25925925925924], [827.0, 358.25925925925924], [829.0, 249.25925925925924], [800.0, 193.25925925925924], [775.0, 184.25925925925924], [740.0, 198.25925925925924]], 'group_id': None, 'shape_type': 'polygon', 'flags': {}}, {'label': 'track', 'points': [[860.0, 190.0], [997.0, 186.0], [998.0, 305.0], [924.0, 320.0], [905.0, 352.0], [877.0, 353.0], [869.0, 245.0], [879.0, 222.0]], 'group_id': None, 'shape_type': 'polygon', 'flags': {}}, {'label': 'car', 'points': [[924.0, 321.0], [905.0, 352.0], [909.0, 388.0], [936.0, 404.0], [959.0, 411.0], [966.0, 431.0], [1000.0, 432.0], [1000.0, 306.0]], 'group_id': None, 'shape_type': 'polygon', 'flags': {}}], 'imagePath': '00000100.jpg', 'imageData': None, 'imageHeight': 563, 'imageWidth': 1000}
version: 4.0.0
flags: {}
imagePath: 00000100.jpg
imageData: None
imageHeight: 563
imageWidth: 1000
shapes: [{'label': 'track', 'points': [[634.0, 203.25925925925924], [604.0, 274.25925925925924], [603.0, 339.25925925925924], [622.0, 362.25925925925924], [639.0, 362.25925925925924], [649.0, 353.25925925925924], [682.0, 382.25925925925924], [733.0, 389.25925925925924], [748.0, 363.25925925925924], [827.0, 358.25925925925924], [829.0, 249.25925925925924], [800.0, 193.25925925925924], [775.0, 184.25925925925924], [740.0, 198.25925925925924]], 'group_id': None, 'shape_type': 'polygon', 'flags': {}}, {'label': 'track', 'points': [[860.0, 190.0], [997.0, 186.0], [998.0, 305.0], [924.0, 320.0], [905.0, 352.0], [877.0, 353.0], [869.0, 245.0], [879.0, 222.0]], 'group_id': None, 'shape_type': 'polygon', 'flags': {}}, {'label': 'car', 'points': [[924.0, 321.0], [905.0, 352.0], [909.0, 388.0], [936.0, 404.0], [959.0, 411.0], [966.0, 431.0], [1000.0, 432.0], [1000.0, 306.0]], 'group_id': None, 'shape_type': 'polygon', 'flags': {}}]
Label: track
Shape Type: polygon
Points: [[634.0, 203.25925925925924], [604.0, 274.25925925925924], [603.0, 339.25925925925924], [622.0, 362.25925925925924], [639.0, 362.25925925925924], [649.0, 353.25925925925924], [682.0, 382.25925925925924], [733.0, 389.25925925925924], [748.0, 363.25925925925924], [827.0, 358.25925925925924], [829.0, 249.25925925925924], [800.0, 193.25925925925924], [775.0, 184.25925925925924], [740.0, 198.25925925925924]]
Point: [634.0, 203.25925925925924]
Point: [604.0, 274.25925925925924]
Point: [603.0, 339.25925925925924]
Point: [622.0, 362.25925925925924]
Point: [639.0, 362.25925925925924]
Point: [649.0, 353.25925925925924]
Point: [682.0, 382.25925925925924]
Point: [733.0, 389.25925925925924]
Point: [748.0, 363.25925925925924]
Point: [827.0, 358.25925925925924]
Point: [829.0, 249.25925925925924]
Point: [800.0, 193.25925925925924]
Point: [775.0, 184.25925925925924]
Point: [740.0, 198.25925925925924]
Label: track
Shape Type: polygon
Points: [[860.0, 190.0], [997.0, 186.0], [998.0, 305.0], [924.0, 320.0], [905.0, 352.0], [877.0, 353.0], [869.0, 245.0], [879.0, 222.0]]
Point: [860.0, 190.0]
Point: [997.0, 186.0]
Point: [998.0, 305.0]
Point: [924.0, 320.0]
Point: [905.0, 352.0]
Point: [877.0, 353.0]
Point: [869.0, 245.0]
Point: [879.0, 222.0]
Label: car
Shape Type: polygon
Points: [[924.0, 321.0], [905.0, 352.0], [909.0, 388.0], [936.0, 404.0], [959.0, 411.0], [966.0, 431.0], [1000.0, 432.0], [1000.0, 306.0]]
Point: [924.0, 321.0]
Point: [905.0, 352.0]
Point: [909.0, 388.0]
Point: [936.0, 404.0]
Point: [959.0, 411.0]
Point: [966.0, 431.0]
Point: [1000.0, 432.0]
Point: [1000.0, 306.0]
参考: