json Python module questions. <SOLVED>

Forum rules
Before you post please read how to get help

json Python module questions. <SOLVED>

Postby Witch on Tue Dec 25, 2012 6:45 pm

I'm trying to learn how to use json a Python module. But I'm having difficulties following and understanding this chapter where Hellmann talks about building my own Classes. I can't make the script work. Can somebody teach me this chapter?

http://www.doughellmann.com/PyMOTW/json/#working-with-your-own-types

json_dump_default.py
Code: Select all
class MyObj(object):
    def __init__(self, s):
        self.s = s
    def __repr__(self):
        return '<MyObj(%s)>' % self.s


import json
import json_myobj

obj = json_myobj.MyObj('instance value goes here')

print 'First attempt'
try:
    print json.dumps(obj)
except TypeError, err:
    print 'ERROR:', err

def convert_to_builtin_type(obj):
    print 'default(', repr(obj), ')'
    # Convert objects to a dictionary of their representation
    d = { '__class__':obj.__class__.__name__,
          '__module__':obj.__module__,
          }
    d.update(obj.__dict__)
    return d

print
print 'With default'
print json.dumps(obj, default=convert_to_builtin_type)



terminal
Code: Select all
$ python json_dump_default.py

Traceback (most recent call last):
  File "json_dump_default.py", line 9, in <module>
    import json_myobj

ImportError: No module named json_myobj
Last edited by Witch on Wed Dec 26, 2012 9:10 am, edited 1 time in total.
User avatar
Witch
Level 1
Level 1
 
Posts: 30
Joined: Sun Dec 09, 2012 9:06 am
Location: Stockholm, Sweden

Linux Mint is funded by ads and donations.
 

Re: json Python module questions.

Postby Witch on Wed Dec 26, 2012 9:10 am

User avatar
Witch
Level 1
Level 1
 
Posts: 30
Joined: Sun Dec 09, 2012 9:06 am
Location: Stockholm, Sweden


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 0 guests