Hi Arthur,<div><br></div><div>Maybe this link helps you: <a href="http://www.doughellmann.com/PyMOTW/json/">http://www.doughellmann.com/PyMOTW/json/</a></div><div><br></div><div><a href="http://www.doughellmann.com/PyMOTW/json/"></a>I used it to learn JSON. =o)</div>
<div><br></div><div>Regards,</div><div>Felipe.<br></div><br><div class="gmail_quote">On Wed, Mar 2, 2011 at 11:24 AM, Peter Otten <span dir="ltr"><__<a href="mailto:peter__@web.de">peter__@web.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">Arthur Mc Coy wrote:<br>
<br>
> Hi all,<br>
><br>
><br>
><br>
> I'm trying an example (in attached file, I mean the bottom of this<br>
> message).<br>
><br>
> First, I create a list of 3 objects. Then I do:<br>
><br>
><br>
> PutJSONObjects(objects)<br>
> objects = GetJSONObjects()<br>
> PutJSONObjects(objects, "objects2.json")<br>
><br>
><br>
> 1) PutJSONObjects(objects) method creates objects.json file (by<br>
> default). It works fine.<br>
> 2) Then objects = GetJSONObjects() method get the file contents and<br>
> return.<br>
><br>
> 3) Finally the script fails on the third method<br>
> PutJSONObjects(objects, "objects2.json")<br>
> saying: AttributeError: 'dict' object has no attribute '__dict__'<br>
><br>
><br>
> That is true, because objects returned by GetJSONObjects() is not a<br>
> list of objects, but simple string....<br>
><br>
> So here is the question, please, how should I DECODE .json file into<br>
> list of python objects so that I will be able to put the copy of these<br>
> objects into a new file called objects2.json ?<br>
><br>
> simplejson docs are hard to follow - without examples.<br>
<br>
</div>I suggest that you use json instead which is part of the standard library<br>
since Python 2.6. The documentation is here:<br>
<br>
<a href="http://docs.python.org/library/json.html" target="_blank">http://docs.python.org/library/json.html</a><br>
<br>
If you know that there are only MyObject instances you need a function to<br>
construct such a MyObject instance from a dictionary. You can then recreate<br>
the objects with<br>
<br>
objects = [object_from_dict(d) for d in json.load(f)]<br>
<br>
or, if all dictionaries correspond to MyObject instances<br>
<br>
objects = json.load(f, object_hook=object_from_dict)<br>
<br>
A general implementation for old-style objects (objects that don't derive<br>
from object) is a bit messy:<br>
<br>
# idea copied from pickle.py<br>
class Empty:<br>
pass<br>
<br>
def object_from_dict(d):<br>
obj = Empty()<br>
obj.__class__ = MyObject<br>
obj.__dict__.update((str(k), v) for k, v in d.iteritems()) # *<br>
return obj<br>
<br>
If you are willing to make MyClass a newstyle class with<br>
<br>
class MyObject(object):<br>
# ...<br>
<br>
the function can be simplified to<br>
<br>
def object_from_dict(d):<br>
obj = object.__new__(MyObject)<br>
obj.__dict__.update((str(k), v) for k, v in d.iteritems()) # *<br>
return obj<br>
<br>
(*) I don't know if unicode attribute names can do any harm,<br>
obj.__dict__.update(d) might work as well.<br>
<div><div></div><div class="h5"><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>