Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

python - Iterate over all items in json object

After loading json from a file via data = json.load(fp) I want to iterate over all items that were in json, excluding of course all special Python symbols. How is this done properly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

data should be an ordinary collection at this point, so you'd iterate over it the same way you'd iterate over any other list/dict/whatever. The fact that it came from load doesn't incur any extra requirements on your part.

Here's an example that uses loads, which is similar in principle:

import json
my_json_data = "[1,2,3]"
data = json.loads(my_json_data)
for item in data:
    print(item)

Result:

1
2
3

Edit: if you're asking "how to I iterate over all the values in my data, including the ones contained inside deeply nested collections?", then you could do something like:

import json
my_json_data = """[
    1,
    {
        "2": 3,
        "4": [
            "5",
            "6",
            "7"
        ]
    },
    8,
    9
]"""

def recursive_iter(obj):
    if isinstance(obj, dict):
        for item in obj.values():
            yield from recursive_iter(item)
    elif any(isinstance(obj, t) for t in (list, tuple)):
        for item in obj:
            yield from recursive_iter(item)
    else:
        yield obj

data = json.loads(my_json_data)
for item in recursive_iter(data):
    print(item)

Result:

1
5
6
7
3
8
9

Edit: You can also keep track of the keys needed to navigate to each value, by passing them through the recursive calls and adding new keys to the collection as you pass over them.

def recursive_iter(obj, keys=()):
    if isinstance(obj, dict):
        for k, v in obj.items():
            yield from recursive_iter(v, keys + (k,))
    elif any(isinstance(obj, t) for t in (list, tuple)):
        for idx, item in enumerate(obj):
            yield from recursive_iter(item, keys + (idx,))
    else:
        yield keys, obj

data = json.loads(my_json_data)
for keys, item in recursive_iter(data):
    print(keys, item)

Result:

(0,) 1
(1, '2') 3
(1, '4', 0) 5
(1, '4', 1) 6
(1, '4', 2) 7
(2,) 8
(3,) 9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...