python 将json数据格式化写入文件

from taxii2client import Server
import json
server = Server('https://cti-taxii.mitre.org/taxii')

api_roots = server.api_roots

for api_root in api_roots:
    collections = api_root.collections
    for collection in collections:
        print(collection.title)
        # collection.get_objects()的数据类型是dict,即是json数据
        #如果其是json字符串,则需要使用json.loads()函数将其解析成json数据
        Objects = json.dumps(collection.get_objects(), indent=4)
        with open('C:/Users/bitsTyrant/Desktop/工作/taxii实验/MITRE\MITRE CTI/' + collection.title + ' Objects.json', 'wt') as f:
            f.write(Objects)
        Manifeset = json.dumps(collection.get_manifest(), indent=4)
        with open('C:/Users/bitsTyrant/Desktop/工作/taxii实验/MITRE\MITRE CTI/' + collection.title + ' Manifest.json', 'wt') as f:
            f.write(Manifeset)

参考:

【Golang】格式化JSON字符串,方便查看

你可能感兴趣的:(python)