简体   繁体   English

如何在python中访问这些json对象

[英]How can I access these json object in python

I'm making some data visualization from movies database api and I already access the data in the normal way but when i load the json data and for loop to print it, the data that out is just the column but I need to access the object inside. 我正在从电影数据库api进行一些数据可视化,我已经以正常方式访问数据但是当我加载json数据并且循环打印它时,输出的数据只是列但我需要访问对象内。

url = "https://api.themoviedb.org/3/discover/movie?api_key="+ api_key 
+"&language=en- US&sort_by=popularity.desc&include_adult=
false&include_video=false&page=1" # api url

response = urllib.request.urlopen(url)
raw_json = response.read().decode("utf-8")
data = json.loads(raw_json)

for j in data:
    print(j)

i expect the output would be 我希望输出会是

[{'popularity': 15,
  'id': 611,
  'video': False,
  'vote_count': 1403,
  'vote_average': 8.9,
  'title': 'lalalalo'},{....}]

but the actual output is 但实际输出是

page
total_results
total_pages
results

The results are one level down. 结果是一级下降。 You are looping through the metadata. 您正在循环元数据。

Try changing your code to 尝试将代码更改为

import json
import urllib.request
api_key = "your api code"

url = "https://api.themoviedb.org/3/discover/movie?api_key=" + api_key +"&language=en- US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1" # api url

response = urllib.request.urlopen(url)
raw_json = response.read().decode("utf-8")
data = json.loads(raw_json)

for j in data['results']:
    print(j)

You need to change 你需要改变

data

to

data['results']

you can simply use requests module... 你可以简单地使用请求模块......

import requests
import json

your_link = " "
r = requests.get(your_link)
data = json.loads(r.content)

You shall have the json loaded up, then use your key "results" ["results"] and loop through the data you got. 你应该加载json,然后使用你的密钥“results”[“results”]并遍历你得到的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM