使用Python解析和打印JSON数据

时间:2016-06-23 08:41:35

标签: python json curl

我是JSON的新手,我正在努力使用Python从JSON数据中提取值。我正在使用另一个带有cURL的shell脚本获取JSON数据。

这是我的shell脚本(Called test.sh)的JSON输出:

{"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"}}

我想将所有“Country”值和“count”值打印成以下内容:

AU 417
BG 7
CA 198
...

为了做到这一点,我创建了一个循环来获取并打印所有需要的值,但是我收到了这个错误:

 AttributeError: 'str' object has no attribute 'read'

这是我的python代码:

import subprocess
import json
import sys
import subprocess
answer = subprocess.check_output(['./test.sh']) #test.sh contains the cURL command
json_obj = json.load(answer)
for i in json_obj['result']:
    print i['Country']
    print i['count']

我在这里错过了什么吗?

任何帮助将不胜感激, 非常感谢你

5 个答案:

答案 0 :(得分:1)

我在帖子中看到了一些错误或错误解释的事情:

  1. 如果你有一组JSON对象,你应该把它们作为一个对象数组,或者一次解析一个作为单独的文件或一个文件中的单独行(不推荐)。前者更容易,更可靠:

    [{"obj":1},{"obj":2},...]
    
  2. 如果您直接从字符串而不是文件加载,则应使用json.loads而不是json.load

  3. 这是一个有效的例子:

    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']
    

答案 1 :(得分:0)

对于结果输出

{"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"}}

实际上有一些doc(单个json)。 你应该逐行阅读,而不是所有行。

这是建议。您可以暂时保存JSON文档,然后阅读它们并加载它们。

  1. 保存test.sh > tmp.txt
  2. 逐行阅读
  3. 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)) 或者,如果您坚持使用子进程,您仍然可以逐行读取文件。记住你得到的输出是一个列表。您应该使用循环来迭代所有它。

答案 2 :(得分:0)

简单的解决方案是, 让我们从你的答案开始:

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"}}'''

将其转换为有效的json

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

打印结果:

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

所以你的完整代码是:

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']

答案 3 :(得分:0)

请将代码保存到test.py,将数据保存到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

你也可以在你的编程中尝试:

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

答案 4 :(得分:-1)

您的数据是多个json对象。

您可能希望将其包装在大括号中({}),并将其视为所有对象的唯一集合。

{
{"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"}}
}