将json字符串转换为python对象 [英] convert a json string to python object
问题描述
是否可以将json字符串(例如,从twitter搜索json服务返回的json字符串)转换为简单的字符串对象.这是从json服务返回的数据的一小幅表示形式:
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:
{
results:[...],
"max_id":1346534,
"since_id":0,
"refresh_url":"?since_id=26202877001&q=twitter",
.
.
.
}
让我们说我以某种方式将结果存储在某个变量中,例如 obj .我正在寻找适当的值,如下所示:
Lets say that I somehow store the result in some variable, say, obj. I am looking to get appropriate values like as follows:
print obj.max_id
print obj.since_id
我尝试使用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'
I've tried using
simplejson.load()
andjson.load()
but it gave me an error saying'str' object has no attribute 'read'
要从字符串加载,请使用json.loads()
(请注意's').
To load from a string, use json.loads()
(note the 's').
更有效地,跳过将响应读取为字符串的步骤,只需将响应传递给json.load()
.
More efficiently, skip the step of reading the response into a string, and just pass the response to json.load()
.
这篇关于将json字符串转换为python对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!