Parsing and printing JSON data using Python

前端 未结 5 1001
臣服心动
臣服心动 2020-12-22 06:32

I\'m new to JSON and I\'m working on extracting values from JSON data using Python. I\'m getting the JSON data using another shell script with cURL.

Here is my JSON

5条回答
  •  囚心锁ツ
    2020-12-22 07:25

    Please save the code into test.py and data into test.json.

    test.py

    import json
    
    
    with open('/tmp/test.json') as f:
        for i in f:
            data = json.loads(i)
            print("{Country} {count}".format(**data["result"]))
    

    test.json

    $ python test.py
    AU 417
    BG 7
    CA 198
    CH 1
    CN 3
    CR 1
    DE 148
    DK 1
    FI 1
    FR 1052
    GB 1430
    HK 243
    VG 54
    

    You can also try it in your prog:

    for i in answer:
        data = json.loads(i)
        print("{Country} {count}".format(**data["result"]))
    

提交回复
热议问题