I'll be receiving a JSON encoded string form Obj-C, and I am decoding a dummy string (for now) like the code below. My output comes out with character 'u' prefixing each item:
我将收到一个JSON编码的字符串形式Obj-C,我正在解码一个虚拟字符串(现在),就像下面的代码一样。我的输出显示了字符u前缀的每一项:
[{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}...
How is JSON adding this unicode char? What's the best way to remove it?
JSON如何添加这个unicode字符?最好的去除方法是什么?
mail_accounts = []
da = {}
try:
s = '[{"i":"imap.gmail.com","p":"aaaa"},{"i":"imap.aol.com","p":"bbbb"},{"i":"333imap.com","p":"ccccc"},{"i":"444ap.gmail.com","p":"ddddd"},{"i":"555imap.gmail.com","p":"eee"}]'
jdata = json.loads(s)
for d in jdata:
for key, value in d.iteritems():
if key not in da:
da[key] = value
else:
da = {}
da[key] = value
mail_accounts.append(da)
except Exception, err:
sys.stderr.write('Exception Error: %s' % str(err))
print mail_accounts
5 个解决方案
#1
111
The u- prefix just means that you have a Unicode string. When you really use the string, it won't appear in your data. Don't be thrown by the printed output.
u-前缀只是表示你有一个Unicode字符串。当您真正使用字符串时,它不会出现在您的数据中。不要被打印出来的输出所抛弃。
For example, try this:
例如,试试这个:
print mail_accounts[0]["i"]
You won't see a u.
你不会看到一个u。
#2
107
Everything is cool, man. The 'u' is a good thing, it indicates that the string is of type Unicode in python 2.x.
一切都很酷,伙计。“u”是一件好事,它表明字符串在python 2.x中是Unicode的类型。
http://docs.python.org/2/howto/unicode.html#the-unicode-type
http://docs.python.org/2/howto/unicode.html the-unicode-type
#3
29
I believe that the d3
print below is the one you were looking for (which is the combination of dumps and loads) :)
我认为下面的d3打印是您正在寻找的(这是转储和负载的组合):
Having:
有:
import json
d = """{"Aa": 1, "BB": "blabla", "cc": "False"}"""
d1 = json.loads(d) # Produces a dictionary out of the given string
d2 = json.dumps(d) # Produces a string out of a given dict or string
d3 = json.dumps(json.loads(d)) # 'dumps' gets the dict from 'loads' this time
print "d1: " + str(d1)
print "d2: " + d2
print "d3: " + d3
Prints:
打印:
d1: {u'Aa': 1, u'cc': u'False', u'BB': u'blabla'}
d2: "{\"Aa\": 1, \"BB\": \"blabla\", \"cc\": \"False\"}"
d3: {"Aa": 1, "cc": "False", "BB": "blabla"}
#4
8
Unicode is an appropriate type here. The JSONDecoder docs describe the conversion table and state that json string objects are decoded into Unicode objects
Unicode在这里是一种合适的类型。JSONDecoder文档描述了转换表,并声明json字符串对象被解码为Unicode对象。
https://docs.python.org/2/library/json.html#encoders-and-decoders
https://docs.python.org/2/library/json.html编码器和译码器
JSON Python
==================================
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None
"encoding determines the encoding used to interpret any str objects decoded by this instance (UTF-8 by default)."
“编码决定了用于解释此实例解码的任何str对象的编码(默认情况下是UTF-8)。”
#5
4
The u
prefix means that those strings are unicode rather than 8-bit strings. The best way to not show the u
prefix is to switch to Python 3, where strings are unicode by default. If that's not an option, the str
constructor will convert from unicode to 8-bit, so simply loop recursively over the result and convert unicode
to str
. However, it is probably best just to leave the strings as unicode.
u前缀意味着这些字符串是unicode而不是8位字符串。不显示u前缀的最好方法是切换到python3,默认情况下字符串是unicode。如果这不是一个选项,则str构造函数将从unicode转换为8位,因此简单地循环遍历结果并将unicode转换为str。但是,最好还是将字符串作为unicode。
#1
111
The u- prefix just means that you have a Unicode string. When you really use the string, it won't appear in your data. Don't be thrown by the printed output.
u-前缀只是表示你有一个Unicode字符串。当您真正使用字符串时,它不会出现在您的数据中。不要被打印出来的输出所抛弃。
For example, try this:
例如,试试这个:
print mail_accounts[0]["i"]
You won't see a u.
你不会看到一个u。
#2
107
Everything is cool, man. The 'u' is a good thing, it indicates that the string is of type Unicode in python 2.x.
一切都很酷,伙计。“u”是一件好事,它表明字符串在python 2.x中是Unicode的类型。
http://docs.python.org/2/howto/unicode.html#the-unicode-type
http://docs.python.org/2/howto/unicode.html the-unicode-type
#3
29
I believe that the d3
print below is the one you were looking for (which is the combination of dumps and loads) :)
我认为下面的d3打印是您正在寻找的(这是转储和负载的组合):
Having:
有:
import json
d = """{"Aa": 1, "BB": "blabla", "cc": "False"}"""
d1 = json.loads(d) # Produces a dictionary out of the given string
d2 = json.dumps(d) # Produces a string out of a given dict or string
d3 = json.dumps(json.loads(d)) # 'dumps' gets the dict from 'loads' this time
print "d1: " + str(d1)
print "d2: " + d2
print "d3: " + d3
Prints:
打印:
d1: {u'Aa': 1, u'cc': u'False', u'BB': u'blabla'}
d2: "{\"Aa\": 1, \"BB\": \"blabla\", \"cc\": \"False\"}"
d3: {"Aa": 1, "cc": "False", "BB": "blabla"}
#4
8
Unicode is an appropriate type here. The JSONDecoder docs describe the conversion table and state that json string objects are decoded into Unicode objects
Unicode在这里是一种合适的类型。JSONDecoder文档描述了转换表,并声明json字符串对象被解码为Unicode对象。
https://docs.python.org/2/library/json.html#encoders-and-decoders
https://docs.python.org/2/library/json.html编码器和译码器
JSON Python
==================================
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None
"encoding determines the encoding used to interpret any str objects decoded by this instance (UTF-8 by default)."
“编码决定了用于解释此实例解码的任何str对象的编码(默认情况下是UTF-8)。”
#5
4
The u
prefix means that those strings are unicode rather than 8-bit strings. The best way to not show the u
prefix is to switch to Python 3, where strings are unicode by default. If that's not an option, the str
constructor will convert from unicode to 8-bit, so simply loop recursively over the result and convert unicode
to str
. However, it is probably best just to leave the strings as unicode.
u前缀意味着这些字符串是unicode而不是8位字符串。不显示u前缀的最好方法是切换到python3,默认情况下字符串是unicode。如果这不是一个选项,则str构造函数将从unicode转换为8位,因此简单地循环遍历结果并将unicode转换为str。但是,最好还是将字符串作为unicode。