如何从python返回的json文件中获取特定密钥?

时间:2019-04-23 12:46:34

标签: python json

我在从python中的json文件中获取特定密钥时遇到麻烦。我真的不知道从哪里开始,因为我以前从未在python中使用json。

[
  {
    "author": "Chinua Achebe",
    "country": "Nigeria",
    "imageLink": "images/things-fall-apart.jpg",
    "language": "English",
    "link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
    "pages": 209,
    "title": "Things Fall Apart",
    "year": 1958
  },
  {
    "author": "Hans Christian Andersen",
    "country": "Denmark",
    "imageLink": "images/fairy-tales.jpg",
    "language": "Danish",
    "link": "https://en.wikipedia.org/wiki/Fairy_Tales_Told_for_Children._First_Collection.\n",
    "pages": 784,
    "title": "Fairy tales",
    "year": 1836
  }
]

这是json文件的几行内容。

import json
f = open('**\\booksset1.json')
data = json.load(f)

这部分加载json文件,但是我将如何从整个文件中获取一个标题。

7 个答案:

答案 0 :(得分:1)

您可以使用.get函数从字典列表中获取键的值,如下所示:

data = [
  {
    "author": "Chinua Achebe",
    "country": "Nigeria",
    "imageLink": "images/things-fall-apart.jpg",
    "language": "English",
    "link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
    "pages": 209,
    "title": "Things Fall Apart",
    "year": 1958
  },
  {
    "author": "Hans Christian Andersen",
    "country": "Denmark",
    "imageLink": "images/fairy-tales.jpg",
    "language": "Danish",
    "link": "https://en.wikipedia.org/wiki/Fairy_Tales_Told_for_Children._First_Collection.\n",
    "pages": 784,
    "title": "Fairy tales",
    "year": 1836
  }
]

titles = [item.get("title", "") for item in data]

输出:

['Things Fall Apart', 'Fairy tales']

答案 1 :(得分:0)

用于循环访问键和值。 例如

import json
f = open('**\\booksset1.json')
data = json.load(f)

Print First item title: 

print data[0]['title']
To iterate all the of the author details
for d in data:
   print d['author]
   print d['title']

答案 2 :(得分:0)

data = [
  {
    "author": "Chinua Achebe",
    "country": "Nigeria",
    "imageLink": "images/things-fall-apart.jpg",
    "language": "English",
    "link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
    "pages": 209,
    "title": "Things Fall Apart",
    "year": 1958
  },
  {
    "author": "Hans Christian Andersen",
    "country": "Denmark",
    "imageLink": "images/fairy-tales.jpg",
    "language": "Danish",
    "link": "https://en.wikipedia.org/wiki/Fairy_Tales_Told_for_Children._First_Collection.\n",
    "pages": 784,
    "title": "Fairy tales",
    "year": 1836
  }
]


# Get all authors in a list (might want to convert to set to remove duplicates)     
authors = [d["author"] for d in data] 
print (authors) 

# Find all books by author 'Chinua Achebe'
for book in data:
    if book['author'] == 'Chinua Achebe':
        print (book)


# Find all books later than year 1950         
for book in data:
    if book['year'] > 1950:
        print (book['title'], book['year'])

答案 3 :(得分:0)

<a (click)="deleteBook(book._id, bookForm)" class="aColorRed"> delete </a> 已从文本字符串表示形式转换为(在这种情况下)词典列表。因此,要访问第二个故事(童话)的json.load,请使用

year

您可以执行所有常见的python事情,例如遍历列表中的所有字典:

print( data[1]['year'] ) # should output 1836

答案 4 :(得分:0)

如果仍然遇到问题,请尝试使用ReadSettings

from readsettings import ReadSettings # Import readsettings library
f = ReadSettings("file.json") # Load the JSON file
print(f[0]["author"]) # Print the value of "author" from the first dictionary in the list

以下是您可以做的其他一些技巧:

f[0]["author"] = "Foo Man" # Set the value
f.json() # Get object from the JSON file

您可以找到更多信息here

答案 5 :(得分:0)

这将使您基本了解如何在python中使用JSON。

import json
f = open('booksset1.json')
data = json.load(f)
# for single 
print("Aurthor name :",data[0]['author'])
print("Country name :",data[0]['country'])
print()
# for multiple you should use loop
for i in data:
    print(i['title'])

答案 6 :(得分:0)

如果您希望在pandas DataFrame中使用此数据,并且由于您的数据的行为就像字典列表一样,则可以尝试:

import pandas as pd

df = pd.DataFrame.from_dict(data)

df

                    author  country  ...               title  year
0            Chinua Achebe  Nigeria  ...   Things Fall Apart  1958
1  Hans Christian Andersen  Denmark  ...         Fairy tales  1836