将json字符串转换为python对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3847399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:02:04  来源:igfitidea点击:

convert a json string to python object

pythonjson

提问by deostroll

Is it possible to convert a json string (for e.g. the one returned from the twitter search json service) to simple string objects. Here is a small representation of data returned from the json service:

是否可以将 json 字符串(例如从 twitter 搜索 json 服务返回的字符串)转换为简单的字符串对象。这是从 json 服务返回的数据的一个小表示:

{
results:[...],
"max_id":1346534,
"since_id":0,
"refresh_url":"?since_id=26202877001&q=twitter",
.
.
.
}

Lets say that I somehow store the result in some variable, say, obj. I am looking to get appropriate values like as follows:

假设我以某种方式将结果存储在某个变量中,例如obj。我希望获得如下适当的值:

print obj.max_id
print obj.since_id

I've tried using simplejson.load()and json.load()but it gave me an error saying 'str' object has no attribute 'read'

我试过使用simplejson.load()json.load()但它给了我一个错误说'str' object has no attribute 'read'

采纳答案by Ben James

I've tried using simplejson.load()and json.load()but it gave me an error saying 'str' object has no attribute 'read'

我试过使用simplejson.load()json.load()但它给了我一个错误说'str' object has no attribute 'read'

To load from a string, use json.loads()(note the 's').

要从字符串加载,请使用json.loads()(注意“s”)。

More efficiently, skip the step of reading the response into a string, and just pass the response to json.load().

更有效地,跳过将响应读入字符串的步骤,只需将响应传递给json.load().

回答by Rex Fender Baird

if you don't know if the data will be a file or a string.... use

如果您不知道数据是文件还是字符串.... 使用

import StringIO as io
youMagicData={
results:[...],
"max_id":1346534,
"since_id":0,
"refresh_url":"?since_id=26202877001&q=twitter",
.
.
.
}

magicJsonData=json.loads(io.StringIO(str(youMagicData)))#this is where you need to fix
print magicJsonData
#viewing fron the center out...
#youMagicData{}>str()>fileObject>json.loads
#json.loads(io.StringIO(str(youMagicData))) works really fast in my program and it would work here so stop wasting both our reputation here and stop down voting because you have to read this twice 

from https://docs.python.org/3/library/io.html#text-i-o

来自https://docs.python.org/3/library/io.html#text-io

json.loads from the python built-in libraries, json.loads requires a file object and does not check what it's passed so it still calls the read function on what you passed because the file object only gives up data when you call read(). So because the built-in string class does not have the read function we need a wrapper. So the StringIO.StringIO function in short, subclasses the string class and the file class and meshing the inner workings hears my low detail rebuild https://gist.github.com/fenderrex/843d25ff5b0970d7e90e6c1d7e4a06b1so at the end of all that its like writing a ram file and jsoning it out in one line....

json.loads 来自 python 内置库,json.loads 需要一个文件对象并且不检查它传递的内容,所以它仍然对你传递的内容调用 read 函数,因为文件对象只在你调用 read() 时放弃数据. 所以因为内置的字符串类没有读取函数,所以我们需要一个包装器。因此,简而言之,StringIO.StringIO 函数将字符串类和文件类进行子类化,并对内部工作进行网格化处理,可以听到我的低细节重建https://gist.github.com/fenderrex/843d25ff5b0970d7e90e6c1d7e4a06b1所以最后它就像写作一个 ram 文件并在一行中将其 jsoning 出来....