吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1014|回复: 3
收起左侧

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

[复制链接]
本路人 发表于 2020-9-30 17:01
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)

[Asm] 纯文本查看 复制代码
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
rsnodame 发表于 2020-10-2 12:48
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 03:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表