lovebay

python之json文件解析

文件json片段:

{"Registration":[{"accessibleNodes":[], 
"name":"wenling025","panotype":"faroscene","index":0,"deltastart":0,"deltaend":2.63894,"position":[18.7219, 37.4723,
78.3677],"compassdirection": -167.823790349,"gps": "65,12114.5,2828.4","time": "17.6.2020 8:28:26:826","angle":
-1.20249,"eulerangle":[-0.00517583, -0.00700516,
-1.20249],"quaternion":{"w":0.824629,"x":-0.000152764,"y":-0.00435222,"z":-0.565657}},{"accessibleNodes":[],
"name":"wenling026","panotype":"faroscene","index":1,"deltastart":0,"deltaend":2.63894,"position":[10.1405, 25.5701,
76.5737],"compassdirection": -36.639810333,"gps": "65,12114.5,2828.4","time": "17.6.2020 8:35:39:84","angle":
2.42307,"eulerangle":[0.014962, -0.00228156,
2.42307],"quaternion":{"w":0.351579,"x":0.00156224,"y":-0.00740439,"z":0.936128}},{"accessibleNodes":[],
"name":"wenling027","panotype":"faroscene","index":2,"deltastart":0,"deltaend":2.63894,"position":[2.21181, 29.4989,
74.2136],"compassdirection": 24.975850619,"gps": "65,12114.5,2828.4","time": "17.6.2020 8:42:56:759","angle":
1.24201,"eulerangle":[0.0163222, 0.013833,
1.24201],"quaternion":{"w":0.813216,"x":0.0106614,"y":0.000876532,"z":0.581864}}

python:

Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:

  • json.dumps(): 对数据进行编码。
  • json.loads(): 对数据进行解码。
  • 如果你要处理的是文件而不是字符串,你可以使用 json.dump()json.load() 来编码和解码JSON数据。

代码:

 1 #!/usr/bin/python3
 2 # -*- coding: UTF-8 -*-
 3 
 4 # noinspection PyUnresolvedReferences
 5 import io
 6 # noinspection PyUnresolvedReferences
 7 from io import BytesIO
 8 # noinspection PyUnresolvedReferences
 9 import binascii
10 # noinspection PyUnresolvedReferences
11 import json
12 # noinspection PyUnresolvedReferences
13 import numpy
14 
15 if __name__ == \'__main__\':
16     # print("hello word ... ")
17     # 读取json文件内容,返回字典格式
18     with open(\'./panorama.json\',\'r\',encoding=\'utf8\')as fp:
19         json_data = json.load(fp)
20 
21     outfile = open("./panorama.txt", "r+")
22     #print("json_data[\'Registration\']:",json_data[\'Registration\'])
23     list_data = json_data[\'Registration\']
24     for list_value in list_data:
25         outfile.write(list_value[\'name\'])
26         outfile.write(\'\n\')
27         for x in list_value[\'position\']:
28             outfile.write(str(x))
29             outfile.write(\' \')
30         outfile.write(\'\n\')
31 
32         for x in list_value[\'eulerangle\']:
33             outfile.write(str(x))
34             outfile.write(\' \')
35         outfile.write(\'0.0\')
36         outfile.write(\'\n\')
37         #print("position :",list_value[\'position\'])
38         #print("eulerangle:",list_value[\'eulerangle\'])
39     fp.close()
40     outfile.close()

 

 

参考:

https://www.runoob.com/python3/python3-json.html

https://www.cnblogs.com/bigberg/p/6430095.html

https://www.cnblogs.com/XhyTechnologyShare/p/12033690.html

 

分类:

技术点:

相关文章: