本路人 发表于 2020-9-30 17:01

python怎么读取修改json并树状格式输出?

RT, 读取的json文件格式如
转换成
读取的json文件格式本来是
"skins": [
        {
                "name": "default",
                "attachments": {
                        "attack": {
                                "attack": { "x": 43.55, "y": -5.92, "rotation": -87.37, "width": 160, "height": 150 }
                        },
                        "body": {
                                "body": { "x": 20.7, "y": -8.53, "rotation": -84.74, "width": 155, "height": 152 }
                        },
                        "body_op": {
                                "body_op": { "x": 5.83, "y": -7.58, "rotation": 66.09, "width": 57, "height": 75 }
                        },
.....
这样简单易读的树状格式, 但是经过我读取再保存后,
with open(json_path,'rb') as f:   
      params = json.load(f)
      params['batch_size'] = 16
      dict = params
......
    with open('output.json', 'w') as f:
      json.dump(fileJson, f)
输出的格式变成了{"skins":[{"name":"default","attachments":{"error":{"error":{"x":43.55,"y":-5.92,"height":150,"width":160,"rotation":-87.37}},"right_leg_2":{"right_leg_2":{"x":9.52,"y":0.91,"height":53,"width":26,"rotation":93.3}},"shadow.....
这样的单行格式..请问如何把json保存这种简单易读的树状结构,有什么插件或者方法吗

pzx521521 发表于 2020-9-30 17:17

本帖最后由 pzx521521 于 2020-9-30 17:22 编辑

indent
json.dumps(dicts,indent=4)

import json

str = '{"name":"BeJson","url":"http://www.bejson.com","page":88,"isNonProfit":true,"address":{"street":"科技园路.","city":"江苏苏州","country":"中国"},"links":[{"name":"Google","url":"http://www.google.com"},{"name":"Baidu","url":"http://www.baidu.com"},{"name":"SoSo","url":"http://www.SoSo.com"}]}'
with open('output.json', 'w') as f:
    my_dict = json.loads(str)
    str_json = json.dump(my_dict, f, indent=4)

Zeaf 发表于 2020-9-30 20:58

找个相关美化json的网站的接口?

rsnodame 发表于 2020-10-2 12:48

{:301_985:} 找个在线格式化代码的网站?
页: [1]
查看完整版本: python怎么读取修改json并树状格式输出?