0

我正在阅读一个 JSON 字符串,其中散布着 u'string' 样式的字符串。例子:

 [
     {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        }, 
        "year": {
          "value": "1996"
        }
      }, 
      "guid": "#9202a8c04000641f80000000003a0ee6", 
      "type": "\/games\/game", 
      "id": "\/en\/el_grande", 
      "name": "El Grande"
    }, 
    {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        }, 
        "year": {
          "value": "1995"
        }
      }, 
      "guid": "#9202a8c04000641f80000000000495ec", 
      "type": "\/games\/game", 
      "id": "\/en\/settlers_of_catan", 
      "name": "Settlers of Catan"
    }
  ]

如果我指定 name = result.name。然后,当我将该值传递给 Django 模板时,它显示为 u'Dominion'

如何格式化它以显示为 Dominion?

++更新++

我认为问题与从列表或字典中打印值有关。例如:

result = freebase.mqlread(query)

games = {}
count = 0
r = result[0]
name = r.name
games["name"] = name,
self.response.out.write(games["name"])
self.response.out.write(name)

这显示为:

(u'Dominion',)  // saved response to dictionary, and then printed
Dominion        // when calling the value directly from the response

我需要遍历一组 JSON 项,并且这些值使用 unicode 显示。为什么?

4

3 回答 3

1
>>> # example
>>> s = u"Jägermütze"
>>> s.encode("utf-8")
'J\xc3\xa4germ\xc3\xbctze'
>>> print s.encode("utf-8") # on a utf-8 terminal
Jägermütze

对 Django 了解不多,但不接受 snicode 字符串对我来说似乎是不符合标准的。

于 2010-12-21T00:10:59.810 回答
1

末尾的逗号games["name"] = name,使其成为 1 元组。去掉它。

于 2010-12-21T01:27:36.320 回答
0

您可以使用它str(your string)来执行此操作。

于 2011-03-03T12:04:16.533 回答