简体   繁体   English

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

[英]How to handle the JSON formatted string?

I use json.dumps() to handle the value and want to display them in the front-end. 我使用json.dumps()处理该值,并希望在前端显示它们。

I set the header to "application/json",but it doesn't work well and the quote is converted to " 我将标头设置为“ application / json”,但效果不佳,引号被转换为" in the browser. 在浏览器中。

How do I convert them to the normal output like {"key": "value"} rather than {"key": "value"} ? 如何将它们转换为正常输出,例如{“ key”:“ value”}而不是{"key": "value"}

This is my url .I use web.py to handle the data. 这是我的网址 。我使用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)

Change return render.callback(html) to return html - no need for the template engine here. 更改return render.callback(html)return html -无需这里的模板引擎。

Also, you might consider using the requests module instead of urllib2. 另外,您可以考虑使用请求模块而不是urllib2。 It's much nicer. 好多了。

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

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