0

我在第二天为此苦苦挣扎,不能再进一步了。我的 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 来做到这一点。有人可以帮我一点吗?

4

3 回答 3

1

Parse out the JSON with the json module, which gives you a Python data structure.

Then loop over the 'test' key and dump each dictionary in that list to a new JSON file:

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)

You can further filter the entries as needed, or use data from the entries to generate a filename; entry['name'] is the name value of each entry, for example.

于 2013-11-13T12:51:37.630 回答
0

You could decode your json and use it as a python element (list and dict)

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

If you want to convert it to str again:

print json.dumps(data['test'][0])
print json.dumps(data['test'][1])
于 2013-11-13T12:52:07.277 回答
0

用 json 做什么?好吧,与 XML 相同:解析它。

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

于 2013-11-13T12:48:18.763 回答