繁体   English   中英

如何处理JSON格式的字符串?

[英]How to handle the JSON formatted string?

我使用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)

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

另外,您可以考虑使用请求模块而不是urllib2。 好多了。

暂无
暂无

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

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