【问题标题】:How to prevent escaping sequence when I set Content-Type to application/json?将 Content-Type 设置为 application/json 时如何防止转义序列?
【发布时间】:2013-11-24 08:11:19
【问题描述】:

我的 API 需要返回 json 类型的东西,所以我将 Content-Type 设置为 application/json?

这是我的url,我不希望字符被转义。

我使用 webpy 框架。

def GET(self):
    web.header('Content-Type', 'application/json')
    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()
    gold = json.loads(html)
    return render.callback(gold)

这是示例输出:

{u'kind': u'Listing'}

我不想让角色转义。

有人可以帮我吗?

【问题讨论】:

  • 好像新浪App对我有问题,但我没有经验,文档都是中文的。
  • @frb thx,我把输出加到问题里了,你能看看吗?

标签: python html json jsonp web.py


【解决方案1】:
# If ensure_ascii is True (the default), 
# all non-ASCII characters in the output are escaped.
gold = json.loads(html, ensure_ascii=0, sort_keys=1, indent=4)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2013-04-07
    • 2013-04-03
    相关资源
    最近更新 更多