簡體   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