处理返回JSON数据中的Unicode字符串

0 投票
1 回答
548 浏览
提问于 2025-04-17 14:28

我正在尝试打印从获取的JSON数据中提取的信息,但这些数据是经过Unicode解码的。我该如何编码(看例子)才能正确显示呢?我对Python非常陌生,没能搞定这个问题,我在Windows 7上使用Python 2.7,在命令行终端中操作。谢谢!

例子:结果>>标题:

'R\u00f6yksopp - 49 Percent' 需要打印成 'Röyksopp - 49 Percent'

"title": "R\u00f6yksopp - 49 Percent",

JSON:

"results": [{
        "style": ["House", "Electro", "Synth-pop"],
        "thumb": "http://api.discogs.com/image/R-90-530519-1236701656.jpeg",
        "format": ["CD", "Maxi-Single"],
        "country": "Europe",
        "barcode": ["5 028589 023420", "BEL/BIEM", "LC 3098"],
        "uri": "/R%C3%B6yksopp-49-Percent/master/30161",
        "label": ["Virgin", "Labels", "Wall Of Sound"],
        "catno": "0946 3378752 0",
        "year": "2005",
        "genre": ["Electronic"],
        "title": "R\u00f6yksopp - 49 Percent",
        "resource_url": "http://api.discogs.com/masters/30161",
        "type": "master",
        "id": 30161
    }

1 个回答

1

环境:Windows 7,默认代码页 = 850,Python 2.7.3

使用你输入的简化版本:

>>> import json
>>> js = """{
...         "style": ["House", "Electro", "Synth-pop"],
...         "title": "R\u00f6yksopp - 49 Percent",
...         "id": 30161
...     }"""
>>>
>>> j = json.loads(js)
>>> j
{u'style': [u'House', u'Electro', u'Synth-pop'], u'id': 30161, u'title': u'R\xf6yksopp - 49 Percent'}
>>> j['title']
u'R\xf6yksopp - 49 Percent'
>>> print j['title']
Röyksopp - 49 Percent
>>>

撰写回答