对象表示法¶
- 文件扩展名
.json
javascript对象表示法(JSON)格式是从javascript派生的。然而,它是一种独立于语言的格式,有许多编程语言,支持简单生成和解析JSON文件,其中包括python。
>>> with open('md_result.json', 'r') as f:
... print(f.read())
...
{
"potentials": {
"Ar": {
"type": "Lennard-Jones",
"parameters": {
"sigma": {
"unit": "pm",
"magnitude": 341
},
"epsilon/k_B": {
"unit": "K",
"magnitude": 120
}
}
},
"He": {
"type": "Lennard-Jones",
"parameters": {
"sigma": {
"unit": "pm",
"magnitude": "256"
},
"epsilon/k_B": {
"unit": "K",
"magnitude": "10.22"
}
}
}
},
"atoms": [
{
"type": "He",
"velocity": [
-29.245,
100.45,
128.28
],
"atom_id": 0,
"position": [
5.7222e-07,
4.8811e-09,
2.0415e-07
]
},
{
"type": "He",
"velocity": [
-199.26,
232.75,
-534.38
],
"atom_id": 1,
"position": [
9.771e-07,
3.6371e-07,
4.7311e-07
]
},
{
"type": "Ar",
"velocity": [
-1.5592,
-378.76,
84.091
],
"atom_id": 2,
"position": [
6.4989e-07,
6.7873e-07,
9.5e-07
]
},
{
"type": "Ar",
"velocity": [
342.82,
156.82,
-38.991
],
"atom_id": 3,
"position": [
5.9024e-08,
3.7138e-07,
7.3455e-08
]
},
{
"type": "He",
"velocity": [
-30.45,
-379.75,
-336.32
],
"atom_id": 4,
"position": [
7.6746e-07,
8.3017e-08,
4.852e-07
]
},
{
"type": "Ar",
"velocity": [
-311.51,
-429.39,
-694.74
],
"atom_id": 5,
"position": [
1.7226e-07,
4.6023e-07,
4.7356e-08
]
},
{
"type": "Ar",
"velocity": [
-82.636,
45.098,
-10.626
],
"atom_id": 6,
"position": [
9.6394e-07,
7.2845e-07,
8.8623e-07
]
},
{
"type": "He",
"velocity": [
158.89,
258.58,
-151.5
],
"atom_id": 7,
"position": [
5.445e-07,
4.6373e-07,
6.227e-07
]
},
{
"type": "He",
"velocity": [
-197.03,
156.74,
-185.2
],
"atom_id": 8,
"position": [
7.9322e-07,
9.47e-07,
3.5194e-08
]
},
{
"type": "Ar",
"velocity": [
-38.65,
-696.32,
216.42
],
"atom_id": 9,
"position": [
2.7797e-07,
1.6487e-07,
8.2403e-07
]
},
{
"type": "He",
"velocity": [
-149.63,
422.88,
-76.309
],
"atom_id": 10,
"position": [
1.1842e-07,
6.3244e-07,
5.0958e-07
]
},
{
"type": "Ar",
"velocity": [
484.57,
-267.41,
-352.54
],
"atom_id": 11,
"position": [
2.0359e-07,
8.3369e-07,
9.6348e-07
]
},
{
"type": "He",
"velocity": [
-231.92,
-99.51,
32.77
],
"atom_id": 12,
"position": [
5.1019e-07,
2.247e-07,
2.3846e-08
]
},
{
"type": "Ar",
"velocity": [
-303.95,
47.316,
222.53
],
"atom_id": 13,
"position": [
3.5383e-07,
8.4581e-07,
7.234e-07
]
},
{
"type": "He",
"velocity": [
233.08,
254.18,
429.83
],
"atom_id": 14,
"position": [
3.8515e-07,
2.894e-07,
5.6028e-07
]
},
{
"type": "He",
"velocity": [
199.63,
203.11,
-425.6
],
"atom_id": 15,
"position": [
1.5842e-07,
9.8225e-07,
5.7859e-07
]
},
{
"type": "He",
"velocity": [
66.341,
222.32,
-97.653
],
"atom_id": 16,
"position": [
3.6831e-07,
7.652e-07,
2.9884e-07
]
},
{
"type": "He",
"velocity": [
90.358,
-67.459,
-64.782
],
"atom_id": 17,
"position": [
2.8696e-07,
1.5129e-07,
6.406e-07
]
},
{
"type": "He",
"velocity": [
71.108,
11.06,
15.912
],
"atom_id": 18,
"position": [
1.0325e-07,
9.9012e-07,
3.4381e-07
]
},
{
"type": "Ar",
"velocity": [
239.19,
173.83,
335.29
],
"atom_id": 19,
"position": [
4.3929e-07,
7.5363e-07,
9.9974e-07
]
}
],
"step": 0,
"boundary_conditions": [
"periodic",
"periodic",
"periodic"
],
"system_size": [
{
"unit": "µm",
"magnitude": 100
},
{
"unit": "µm",
"magnitude": 200
},
{
"unit": "µm",
"magnitude": 300
}
],
"time": {
"unit": "s",
"magnitude": 0
},
"atom_types": {
"Ar": "Argon",
"He": "Helium"
}
}
可以很容易地用 json
python标准库模块:
>>> import json
>>> with open('md_result.json', 'r') as f:
... md_data = json.load(f)
...
>>> print(md_data)
{'potentials': {'He': {'parameters': {'sigma': {'magnitude': '256', 'unit': 'pm'}, 'epsilon/k_B': {'magnitude': '10.22', 'unit': 'K'}}, 'type': 'Lennard-Jones'}, 'Ar': {'parameters': {'sigma': {'magnitude': 341, 'unit': 'pm'}, 'epsilon/k_B': {'magnitude': 120, 'unit': 'K'}}, 'type': 'Lennard-Jones'}}, 'time': {'magnitude': 0, 'unit': 's'}, 'boundary_conditions': ['periodic', 'periodic', 'periodic'], 'atom_types': {'He': 'Helium', 'Ar': 'Argon'}, 'system_size': [{'magnitude': 100, 'unit': 'µm'}, {'magnitude': 200, 'unit': 'µm'}, {'magnitude': 300, 'unit': 'µm'}], 'atoms': [{'position': [5.7222e-07, 4.8811e-09, 2.0415e-07], 'atom_id': 0, 'type': 'He', 'velocity': [-29.245, 100.45, 128.28]}, {'position': [9.771e-07, 3.6371e-07, 4.7311e-07], 'atom_id': 1, 'type': 'He', 'velocity': [-199.26, 232.75, -534.38]}, {'position': [6.4989e-07, 6.7873e-07, 9.5e-07], 'atom_id': 2, 'type': 'Ar', 'velocity': [-1.5592, -378.76, 84.091]}, {'position': [5.9024e-08, 3.7138e-07, 7.3455e-08], 'atom_id': 3, 'type': 'Ar', 'velocity': [342.82, 156.82, -38.991]}, {'position': [7.6746e-07, 8.3017e-08, 4.852e-07], 'atom_id': 4, 'type': 'He', 'velocity': [-30.45, -379.75, -336.32]}, {'position': [1.7226e-07, 4.6023e-07, 4.7356e-08], 'atom_id': 5, 'type': 'Ar', 'velocity': [-311.51, -429.39, -694.74]}, {'position': [9.6394e-07, 7.2845e-07, 8.8623e-07], 'atom_id': 6, 'type': 'Ar', 'velocity': [-82.636, 45.098, -10.626]}, {'position': [5.445e-07, 4.6373e-07, 6.227e-07], 'atom_id': 7, 'type': 'He', 'velocity': [158.89, 258.58, -151.5]}, {'position': [7.9322e-07, 9.47e-07, 3.5194e-08], 'atom_id': 8, 'type': 'He', 'velocity': [-197.03, 156.74, -185.2]}, {'position': [2.7797e-07, 1.6487e-07, 8.2403e-07], 'atom_id': 9, 'type': 'Ar', 'velocity': [-38.65, -696.32, 216.42]}, {'position': [1.1842e-07, 6.3244e-07, 5.0958e-07], 'atom_id': 10, 'type': 'He', 'velocity': [-149.63, 422.88, -76.309]}, {'position': [2.0359e-07, 8.3369e-07, 9.6348e-07], 'atom_id': 11, 'type': 'Ar', 'velocity': [484.57, -267.41, -352.54]}, {'position': [5.1019e-07, 2.247e-07, 2.3846e-08], 'atom_id': 12, 'type': 'He', 'velocity': [-231.92, -99.51, 32.77]}, {'position': [3.5383e-07, 8.4581e-07, 7.234e-07], 'atom_id': 13, 'type': 'Ar', 'velocity': [-303.95, 47.316, 222.53]}, {'position': [3.8515e-07, 2.894e-07, 5.6028e-07], 'atom_id': 14, 'type': 'He', 'velocity': [233.08, 254.18, 429.83]}, {'position': [1.5842e-07, 9.8225e-07, 5.7859e-07], 'atom_id': 15, 'type': 'He', 'velocity': [199.63, 203.11, -425.6]}, {'position': [3.6831e-07, 7.652e-07, 2.9884e-07], 'atom_id': 16, 'type': 'He', 'velocity': [66.341, 222.32, -97.653]}, {'position': [2.8696e-07, 1.5129e-07, 6.406e-07], 'atom_id': 17, 'type': 'He', 'velocity': [90.358, -67.459, -64.782]}, {'position': [1.0325e-07, 9.9012e-07, 3.4381e-07], 'atom_id': 18, 'type': 'He', 'velocity': [71.108, 11.06, 15.912]}, {'position': [4.3929e-07, 7.5363e-07, 9.9974e-07], 'atom_id': 19, 'type': 'Ar', 'velocity': [239.19, 173.83, 335.29]}], 'step': 0}
因此,解析JSON文件会导致字典、列表、字符串、整数和浮点数的组合。
编写JSON文件同样简单:
>>> with open('my_md_result.json', 'w') as f:
... json.dump(md_data, f, indent=2)
...
与 indent
关键字导致文件不是以非常简洁的方式写入,而是用换行符和2的缩进。请注意,可以从框中转换以下Python类型:
Python |
JSON |
---|---|
对象 |
|
数组 |
|
一串 |
|
数 |
|
真 |
|
假 |
|
无效的 |