Parsing and printing JSON data using Python

前端 未结 5 1002
臣服心动
臣服心动 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:04

    For the result output

    {"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}
    {"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}
    {"preview":true,"offset":2,"result":{"Country":"CA","count":"198"}}
    {"preview":true,"offset":3,"result":{"Country":"CH","count":"1"}}
    {"preview":true,"offset":4,"result":{"Country":"CN","count":"3"}}
    {"preview":true,"offset":5,"result":{"Country":"CR","count":"1"}}
    

    Actually there are lines of doc ( single json ). You should read them line by line, not by all lines.

    Here's the suggestion. You can temporarily save your JSON docs and then read them and load them.

    1. save it test.sh > tmp.txt
    2. read it line by line

    with open('tmp.txt', 'r') as f: for i in f: doc = f.readline() try: the_dict = json.loads(doc) print(the_dict['Country']) except Exception, e: print(str(e)) Or if you insist on using subprocess, you can still read the file line by line. Just remember the output you got is a list. You should use a loop to iterate all of it.

    0 讨论(0)
  • 2020-12-22 07:19

    Your data is multiple json objects.

    You may want to wrap it in curly brackets ({ and }) and treat it as one unique set of all your objects.

    {
    {"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}
    {"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}
    {"preview":true,"offset":2,"result":{"Country":"CA","count":"198"}}
    {"preview":true,"offset":3,"result":{"Country":"CH","count":"1"}}
    {"preview":true,"offset":4,"result":{"Country":"CN","count":"3"}}
    {"preview":true,"offset":5,"result":{"Country":"CR","count":"1"}}
    {"preview":true,"offset":6,"result":{"Country":"DE","count":"148"}}
    {"preview":true,"offset":7,"result":{"Country":"DK","count":"1"}}
    {"preview":true,"offset":8,"result":{"Country":"FI","count":"1"}}
    {"preview":true,"offset":9,"result":{"Country":"FR","count":"1052"}}
    {"preview":true,"offset":10,"result":{"Country":"GB","count":"1430"}}
    {"preview":true,"offset":11,"result":{"Country":"HK","count":"243"}}
    {"preview":false,"offset":12,"lastrow":true,"result":{"Country":"VG","count":"54"}}
    }
    
    0 讨论(0)
  • 2020-12-22 07:20

    Simple solution would be, Let's start with your answer:

    answer = '''{"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}
    {"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}
    {"preview":true,"offset":2,"result":{"Country":"CA","count":"198"}}
    {"preview":true,"offset":3,"result":{"Country":"CH","count":"1"}}
    {"preview":true,"offset":4,"result":{"Country":"CN","count":"3"}}
    {"preview":true,"offset":5,"result":{"Country":"CR","count":"1"}}
    {"preview":true,"offset":6,"result":{"Country":"DE","count":"148"}}
    {"preview":true,"offset":7,"result":{"Country":"DK","count":"1"}}
    {"preview":true,"offset":8,"result":{"Country":"FI","count":"1"}}
    {"preview":true,"offset":9,"result":{"Country":"FR","count":"1052"}}
    {"preview":true,"offset":10,"result":{"Country":"GB","count":"1430"}}
    {"preview":true,"offset":11,"result":{"Country":"HK","count":"243"}}
    {"preview":false,"offset":12,"lastrow":true,"result":{"Country":"VG","count":"54"}}'''
    

    convert it to a valid json

    import json
    answer = json.loads('['+answer.replace('\n',',')+']')
    

    print your results:

    for i in answer:
        print i['result']['Country'],i['result']['count']
    
    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
    

    So your full code is:

    import subprocess
    import json
    answer = subprocess.check_output(['./test.sh'])
    answer = json.loads('['+answer.replace('\n',',')+']')
    for i in answer:
        print i['result']['Country'],i['result']['count']
    
    0 讨论(0)
  • 2020-12-22 07:21

    I see several things either wrong or misinterpreted in the post:

    1. If you have a collection of JSON objects, you should make them an array of objects, or parse them one at a time as separate files or separate lines in one file (not recommended). The former would be easier and more reliable:

      [{"obj":1},{"obj":2},...]
      
    2. You should use json.loads and not json.load if you're loading directly from a string and not a file.

    3. Here's a working example:

      import json
      
      answers = '[{"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}' + \
                ',{"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}]'
      json_obj = json.loads(answers)
      
      for i in json_obj:
          print i['result']['Country'], i['result']['count']
      
    0 讨论(0)
  • 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"]))
    
    0 讨论(0)
提交回复
热议问题