反序列化JSON数据文件

时间:2018-05-15 01:58:45

标签: python json python-2.7

我很难在python 2.7中反序列化输入数据文件,并且真的不知道从哪里开始。我将输入文件保存为JSON:

{
    "Group": {
        "Test": {
            "test1": {
                "x1": 100,
                "y1": 150
            },
            "test2": {
                "x1": 23,
                "y1": 56,
                "x2": 200,
                "y2": 234
            },
            "test3": {
                "x1": 123,
                "y1": 456,
                "x2": 345,
                "y2": 986,
                "x3": 234,
                "y3": 654
            },
            "test4": {
                "x1": 789,
                "y1": 987,
                "x2": 345,
                "y2": 555,
                "x3": 111,
                "y3": 222,
                "x4": 333,
                "y4": 444
            }
        }
    }
}

我试图删除数据,所以如果情况" test1"需要我可以取x1和y1的值并填充一个字段。如果需要3对x和y值,我需要从" test3"中获取值。

我试图在代码中填充以下变量中的字段。它被保存为字典,因为最终产品是JSON文件。我需要使用上面列出的数据文件中的值填充宽度和高度字段。这是使用json.dumps打印出来的可读性变量:

{
    "subs": {
        "1": {
            "video_info": {
                "width": x1,
                "height": y1
            }
        },
        "2": {
            "video_info": {
                "width": x2,
                "height": y2
            }
        },
        "3": {
            "video_info": {
                "width": x3,
                "height": y3
            }
        }
    }
}

上面的变量是通过从不同的对象(每个字典下的" subs")中提取数据拼凑在一起的,所以我不能从头开始将整个事物写成字典。 " subs"中的每个项目已经有一个需要覆盖的宽度和高度值。

反序列化的例子我见过所有人都设置了密钥。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

d={
    "Group": {
        "Test": {
            "test1": {
                "x1": 100,
                "y1": 150
            },
            "test2": {
                "x1": 23,
                "y1": 56,
                "x2": 200,
                "y2": 234
            },
            "test3": {
                "x1": 123,
                "y1": 456,
                "x2": 345,
                "y2": 986,
                "x3": 234,
                "y3": 654
            },
            "test4": {
                "x1": 789,
                "y1": 987,
                "x2": 345,
                "y2": 555,
                "x3": 111,
                "y3": 222,
                "x4": 333,
                "y4": 444
            }
        }
    }
}
d2={"subs": {}}
for i in range(1,4):
  s=str(i)
  d2["subs"][s]={}
  t=d2["subs"][s]['video_info']={}
  t['width']=d['Group']['Test'][f'test{i}'][f'x{s}']
  t['height']=d['Group']['Test'][f'test{i}'][f'y{s}']
print(d2)

答案 1 :(得分:0)

您可以将其保存到变量并调用它以查看test1-4

import json
json_data = json.dumps({
    "Group": {
        "Test": {
            "test1": {
                "x1": 100,
                "y1": 150
            },
            "test2": {
                "x1": 23,
                "y1": 56,
                "x2": 200,
                "y2": 234
            },
            "test3": {
                "x1": 123,
                "y1": 456,
                "x2": 345,
                "y2": 986,
                "x3": 234,
                "y3": 654
            },
            "test4": {
                "x1": 789,
                "y1": 987,
                "x2": 345,
                "y2": 555,
                "x3": 111,
                "y3": 222,
                "x4": 333,
                "y4": 444
            }
        }
    }
})

json = json.loads(json_data)
print json["Group"]["Test"]["test4"]
相关问题