1

我使用 json.dumps() 来处理值并希望在前端显示它们。

我将标头设置为“application/json”,但效果不佳,并且报价"在浏览器中转换为。

如何将它们转换为正常输出,例如 {"key": "value"} 而不是 {"key": "value"}

这是我的网址。我使用 web.py 来处理数据。

import json
import os
import urllib2
import web

app_root = os.path.dirname(__file__)
templates_root = os.path.join(app_root, 'templates')
render = web.template.render(templates_root)

class Callback:
    def GET(self):
        web.header('Content-Type', 'application/json; charset=utf-8')
        url = "http://www.reddit.com/r/pics/hot.json"
        hdr = { 'User-Agent' : 'super happy flair bot by /u/spladug' }
        req = urllib2.Request(url, headers=hdr)
        html = urllib2.urlopen(req).read()
        html = json.dumps(html)  
        func_name = web.input()['callback']
        html = '{0}({1})'.format(func_name, html)
        return render.callback(html)
4

1 回答 1

1

更改return render.callback(html)return html- 这里不需要模板引擎。

此外,您可以考虑使用requests模块而不是 urllib2。它好多了。

于 2013-11-13T02:12:16.647 回答