Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > parsing json using simplejson

Reply
Thread Tools

parsing json using simplejson

 
 
deostroll
Guest
Posts: n/a
 
      06-15-2009
I need to be able to parse a json data object using the simplejson
package. First of all I need to know all the task needed for this job.

--deostroll
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      06-15-2009
deostroll wrote:

> I need to be able to parse a json data object using the simplejson
> package. First of all I need to know all the task needed for this job.


- install simplejson
- read documentation of simplejson
- use simplejson as documented
- ???
- profit!

Diez
 
Reply With Quote
 
 
 
 
Mike Kazantsev
Guest
Posts: n/a
 
      06-15-2009
On Sun, 14 Jun 2009 22:45:38 -0700 (PDT)
deostroll <(E-Mail Removed)> wrote:

> I need to be able to parse a json data object using the simplejson
> package. First of all I need to know all the task needed for this job.


Note that py2.6 has a bundled json module.

--
Mike Kazantsev // fraggod.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAko2cOQACgkQASbOZpzyXnHSJgCgv6/POJu1Lw3Nx8mbwyQxBKVW
lwMAnRfWUZ22bf2zx4ZixeGTbpTcjJr7
=RqCP
-----END PGP SIGNATURE-----

 
Reply With Quote
 
deostroll
Guest
Posts: n/a
 
      06-16-2009
I want to be able to parse it into python objects. Any ideas?

--deostroll
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      06-16-2009
On Jun 15, 8:01*pm, deostroll <(E-Mail Removed)> wrote:
> I want to be able to parse it into python objects. Any ideas?


1. If applicable, pay better attention in class.
2. Install simplejson and try to use it, then, if you still need help,
come back and post your question along with your honest attempt to do
it.

P.S. Someone will be by to post the "How to ask smart questions" essay
shortly.


Carl Banks
 
Reply With Quote
 
Mike Kazantsev
Guest
Posts: n/a
 
      06-16-2009
On Mon, 15 Jun 2009 20:01:58 -0700 (PDT)
deostroll <(E-Mail Removed)> wrote:

> I want to be able to parse it into python objects. Any ideas?


JSON objects behave like python dicts (key:val pairs), so why not just
use them?

Both simplejson and py2.6-json (which is quite similar to the former)
do just that, but if you like JS attribute-like key access model you
can use it by extending the builtin dict class:


import types, collections


class AttrDict(dict):
'''AttrDict - dict with JS-like key=attr access'''
def __init__(self, *argz, **kwz):
if len(argz) == 1 and not kwz and isinstance(argz[0], types.StringTypes):
super(AttrDict, self).__init__(open(argz[0]))
else:
super(AttrDict, self).__init__(*argz, **kwz)
for k,v in self.iteritems(): setattr(self, k, v) # re-construct all values via factory

def __val_factory(self, val):
return AttrDict(val) if isinstance(val, collections.Mapping) else val

def __getattr__(self, k):
return super(AttrDict, self).__getitem__(k)
__getitem__ = __getattr__

def __setattr__(self, k, v):
return super(AttrDict, self).__setitem__(k, self.__val_factory(v))
__setitem__ = __setattr__


if __name__ == '__main__':
import json

data = AttrDict(json.loads('{"host": "docs.python.org",'
' "port": 80,'
' "references": [ "collections",'
' "json",'
' "types",'
' "data model" ],'
' "see_also": { "UserDict": "similar, although'
' less flexible dict implementation." } }'))

print data.references

# You can always use it as a regular dict
print 'port' in data
print data['see_also']

# Data model propagnates itself to any sub-mappings
data.see_also.new_item = dict(x=1, y=2)
print data.see_also.keys()
data.see_also.new_item['z'] = 3
print data.see_also.new_item.z


--
Mike Kazantsev // fraggod.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAko3IBEACgkQASbOZpzyXnGhkACfQh87il4F0g AI2Lls5n2vo1nV
ObkAnRfhxzYZIvSJ+hBJAQIqAzOqkRht
=7Gb9
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
Re: using simplejson.dumps to encode a dictionary to json Aaron Python 1 03-15-2011 08:25 PM
using simplejson.dumps to encode a dictionary to json Aaron Python 2 03-15-2011 08:22 PM
json vs. simplejson Thomas Heller Python 4 05-13-2009 06:58 PM
"JSON for ASP" at json.org Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM



Advertisments