deostroll deostroll - 4 months ago 13
Python Question

convert a json string to python object

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",
.
.
.
}


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


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

Answer

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

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

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

Comments