如何使用Python将节点从第一个JSON提取到第二个JSON?

时间:2013-11-13 12:42:09

标签: python json

第二天我正在努力奋斗,不能再进一步了。我的JSON是:

{
    "test": [
        {
            "features": [
                "none"
            ],
            "AAA": [
                {
                    "BBB": {
                        "CCC": 95,
                        "DDD": 60
                    },
                    "EEE": "123",
                    "FFF": "image/png",
                    "GGG": "222",
                    "HHH": "image"
                },
                {
                    "BBB": {
                        "CCC": 95,
                        "DDD": 60
                    },
                    "EEE": "126",
                    "FFF": "image/jpg",
                    "GGG": "645",
                    "HHH": "image"
                }
            ],
            "III": [],
            "JJJ": {
                "KKK": true
            },
            "LLL": "0",
            "MMM": false,
            "name": "AXA"
        },
        {
            "features": [
                "none"
            ],
            "AAA": [
                {
                    "BBB": {
                        "CCC": 95,
                        "DDD": 60
                    },
                    "EEE": "123",
                    "FFF": "image/png",
                    "GGG": "222",
                    "HHH": "image"
                },
                {
                    "BBB": {
                        "CCC": 95,
                        "DDD": 60
                    },
                    "EEE": "126",
                    "FFF": "image/jpg",
                    "GGG": "645",
                    "HHH": "image"
                }
            ],
            "III": [],
            "JJJ": {
                "SSS": {
                    "Tech": "ABC",
                    "Tech2": "DEF",
                    "Tech3": "GHI"
                },
                "TTT": {
                    "Tech": "ABC",
                    "Tech2": "DEF",
                    "Tech3": "GHI"
                },
                "UUU": {
                    "Tech": "ABC",
                    "Tech2": "DEF",
                    "Tech3": "GHI"
                },
                "WWW": {
                    "Tech": "ABC",
                    "Tech2": "DEF",
                    "Tech3": "GHI"
                    "Tech4": "JKL"
                },
                "KKK": true
            },
            "PERM": {
                "RRR": false
            },
            "X1": "adsada",
            "X2": false,
            "name": "AXA 2"
        }
    ]
}

我想将完整节点提取到几个JSON文件中。

从我的JSON中我想要提取第3行和第36行之间以及37到94之间的节点。

事情是 - 它不会总是一样的,我的意思是我不能硬编码把它切成两半;)。

我知道XML很简单,但我仍然不知道如何用JSON做到这一点。有人能帮我一点吗?

3 个答案:

答案 0 :(得分:1)

使用json module解析JSON,它为您提供了Python数据结构。

然后遍历'test'密钥并将该列表中的每个字典转储到新的JSON文件中:

import json

with open(inputjsonfile, 'r') as ifh:
    data = json.load(ifh)

    for i, entry in enumerate(data['test']):
        with open('outputfile-test-{}.json'.format(i), 'w') as ofh:
            json.dump(entry, ofh)

您可以根据需要进一步过滤条目,或使用条目中的数据生成文件名;例如,entry['name']是每个条目的名称值。

答案 1 :(得分:0)

json怎么办?嗯,与XML相同:解析它。

=> http://docs.python.org/2/library/json.html

答案 2 :(得分:0)

您可以解码您的json并将其用作python元素(listdict

import json
data = json.loads(my_json)
print data['test'][0] # This would be your element between the lines 3 and 36
print data['test'][1] # This would be from 37 to 94

如果您想再次将其转换为str

print json.dumps(data['test'][0])
print json.dumps(data['test'][1])