简体   繁体   English

将json字符串转换为python对象

[英]convert a json string to python object

Is it possible to convert a json string (for eg the one returned from the twitter search json service) to simple string objects. 是否可以将json字符串(例如,从twitter搜索json服务返回的字符串)转换为简单的字符串对象。 Here is a small representation of data returned from the json service: 以下是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 . 让我们说我以某种方式将结果存储在一些变量中,比如obj I am looking to get appropriate values like as follows: 我希望获得如下适当的值:

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'

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()

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-io 来自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(). json.loads来自python内置库,json.loads需要一个文件对象并且不检查它传递的是什么,所以它仍然会调用你传递的函数的read函数,因为文件对象只在你调用read()时放弃了数据。 So because the built-in string class does not have the read function we need a wrapper. 因为内置的字符串类没有read函数,我们需要一个包装器。 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/843d25ff5b0970d7e90e6c1d7e4a06b1 so at the end of all that its like writing a ram file and jsoning it out in one line.... 所以StringIO.StringIO函数简而言之,子类化字符串类和文件类,并将内部工作网格化,听到我的低细节重建https://gist.github.com/fenderrex/843d25ff5b0970d7e90e6c1d7e4a06b1所以最后它就像写作一样一个ram文件并将其排成一行....

magicJsonData=json.loads(io.StringIO((youMagicData).decode("utf-8"))
print(magicJsonData)

The json string from any request or http server is of type byte array to convert them in to string, (since the question is about byte array returned from a server request, right?) 来自任何请求或http服务器的json字符串都是字节数组,以将它们转换为字符串,(因为问题是关于从服务器请求返回的字节数组,对吧?)

res = json.loads((response.content).decode("utf-8") )
print(res)

here the response.content can be a byte array or any returned string from server request which is decoded to string (utf-8) format and returned as python array. 这里的response.content可以是一个字节数组或来自服务器请求的任何返回字符串,它被解码为字符串(utf-8)格式并作为python数组返回。

Or just use a bytearray but use json.load instead json.loads 或者只使用bytearray但使用json.load而不是json.loads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM