Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python以HTML显示JSON数据_Python_Html_Json - Fatal编程技术网

使用python以HTML显示JSON数据

使用python以HTML显示JSON数据,python,html,json,Python,Html,Json,我有来自url的json,我希望它显示在html列表中: import webapp2 import urllib2 import json from google.appengine.api import users from optparse import OptionParser response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UC

我有来自url的json,我希望它显示在html列表中:

import webapp2
import urllib2
import json
from google.appengine.api import users
from optparse import OptionParser

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCvS6-K6Ydmb4gH-kim3AmjA&maxResults=25&key=AIzaSyB-flU4eaoVByMVbn5RgDxPSKGFjSUhsVw')

data = response.read()

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(data)

如何迭代数据变量并在html列表中显示某些项?

使用
json.loads将json数据加载到python dict中

import json

...

json_data = response.read()
data = json.loads(json_data)
print('<ul>')
for key in data:
    print('<li> %s = %s </li>', (key, data[key]))
print('</ul>')
导入json
...
json_data=response.read()
data=json.load(json_数据)
打印(“
    ”) 对于输入数据: 打印(“
  • %s=%s
  • ”,(键,数据[key])) 打印(“
”)
请注意,如果JSON数据是一个列表而不是一个字典,那么这应该会失败,但它可以很容易地进行调整。但这对于OPs用例是有效的,因为YouTube API返回一个字典()。