当我使用json.loads时你有什么用? [重复]

时间:2022-02-02 07:04:18

This question already has an answer here:

这个问题在这里已有答案:

I've been writing a Python script to parse JSON information from the Soundcloud API, and I was just wondering what the "u"'s are when I use json.loads( val ) and how to store the JSON information to an object without the u's?

我一直在编写一个Python脚本来解析Soundcloud API中的JSON信息,当我使用json.loads(val)以及如何将JSON信息存储到一个对象时,我只是想知道“u”是什么。美国?

i.e. why are there u's in this:

也就是说你为什么这样:

>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]

See the "Decoding JSON" section here to understand what I mean further:

请参阅此处的“解码JSON”部分以了解我的意思:

http://docs.python.org/library/json.html

http://docs.python.org/library/json.html

Thank you!

谢谢!

2 个解决方案

#1


15  

Unicode strings. See the Python Tutorial.

Unicode字符串。请参阅Python教程。

In Python source code, Unicode literals are written as strings prefixed with the ‘u’ or ‘U’ character: u'abcdefghijk'.

在Python源代码中,Unicode文字被编写为前缀为“u”或“U”字符的字符串:u'abcdefghijk'。

Unicode Literals in Python Source Code

- Python源代码中的Unicode文字

#2


7  

the u's are there to indicate that a Unicode string is supposed to be created.

你在那里表明应该创建一个Unicode字符串。

It sucks that json.dump converts strings to unicode strings and leaves no trace of having done that, because then json.load can't convert back.

很糟糕的是json.dump将字符串转换为unicode字符串并且没有留下任何痕迹,因为那时json.load无法转换回来。

To convert to string objects, use PyYAML:

要转换为字符串对象,请使用PyYAML:

>>> import yaml
>>> yaml.load('["foo", {"bar":["baz", null, 1.0, 2]}]')
>>> ['foo', {'bar': ['baz', None, 1.0, 2]}]

But careful! If for some reason you json.dumped an object containing object strings and unicode strings, yaml will load everything as object strings (though that's json.dump's fault really)

但小心!如果由于某种原因你json.dumped一个包含对象字符串和unicode字符串的对象,yaml会将所有内容作为对象字符串加载(虽然这真的是json.dump的错误)

#1


15  

Unicode strings. See the Python Tutorial.

Unicode字符串。请参阅Python教程。

In Python source code, Unicode literals are written as strings prefixed with the ‘u’ or ‘U’ character: u'abcdefghijk'.

在Python源代码中,Unicode文字被编写为前缀为“u”或“U”字符的字符串:u'abcdefghijk'。

Unicode Literals in Python Source Code

- Python源代码中的Unicode文字

#2


7  

the u's are there to indicate that a Unicode string is supposed to be created.

你在那里表明应该创建一个Unicode字符串。

It sucks that json.dump converts strings to unicode strings and leaves no trace of having done that, because then json.load can't convert back.

很糟糕的是json.dump将字符串转换为unicode字符串并且没有留下任何痕迹,因为那时json.load无法转换回来。

To convert to string objects, use PyYAML:

要转换为字符串对象,请使用PyYAML:

>>> import yaml
>>> yaml.load('["foo", {"bar":["baz", null, 1.0, 2]}]')
>>> ['foo', {'bar': ['baz', None, 1.0, 2]}]

But careful! If for some reason you json.dumped an object containing object strings and unicode strings, yaml will load everything as object strings (though that's json.dump's fault really)

但小心!如果由于某种原因你json.dumped一个包含对象字符串和unicode字符串的对象,yaml会将所有内容作为对象字符串加载(虽然这真的是json.dump的错误)