从JSON中获取多个字段[Python]

时间:2014-07-27 16:56:31

标签: python json list

我有一些结构如下所述的JSON文件:

{
    "paragraphs": [
        "Fake anti-virus software that infect PCs with malicious code are a growing threat, according to a study by Google.", 
        "Its analysis of 240m web pages over 13 months showed that fake anti-virus programs accounted for 15% of all malicious software.", 
        "Scammers trick people into downloading programs by convincing them that their PC is infected with a virus.", 
    ], 
    "description": "Google has found ...", 
    "title": "Google warning on fake anti-virus software"
},

我需要迭代并从多个实例中获取所有标题字段(如同一个JSON文件)并将它们存储在新列表中。有人可以帮助我在Python中如何做到这一点吗?如果你能在段落字段上做到这一点,我会有很多好处,而且我有多个条目而不是一个。

2 个答案:

答案 0 :(得分:1)

  1. 使用json模块将您的json文件解析为嵌套dicts和列表的结构。
  2. 使用list comprehensions提取您的说明,例如

    titles = [x['title'] for x in parsed_json]
    

答案 1 :(得分:0)

我确实喜欢:

with open("out.json") as j:
    json_data = j.read()
    data = json.loads(json_data)

for x in range(0,len(data)):
    print data[x]['title']

它有效。现在只有如何获取每个段落,因为上面显示的每个段落部分都有多个实例。