On 11/12/14 20:25, Galen Seilis wrote:
To whom it may concern,
I am having difficulty interacting with JSON. The example below if a
typical input and output:
*import json*
*array = json.load( { "name": "Joe", "address": "111 Street" } )*
*Traceback (most recent call last): File "<stdin>" , line 1, in <module>
File "C:\Python27\lib\json\__init__.py" , line 286, in load return
loads(fp.read(), AttributeError: 'dict' object has no attribute 'read' >>>*
The documentation for json.load() says:
######################
json.load(fp, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize fp (a .read()-supporting file-like object containing a JSON
document) to a Python object using this conversion table.
...
#####################
So it expects a file-like object as its first argument but you are
passing a dictionary.
It's not clear whether you are trying to convert your dictionary into a
json data stream or whether you are trying to read a json string and
convert it to a Python dictionary. Whichever way round you won't get
an array back.
dumps() will turn a Python data structure into a JSON string.
loads() will turn a JSON string into a Python object. I'm guessing
this is what you wanted?
Just remember that the 's' at the end signifies string not a plural.
And the non-s versions use files not strings.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor