How to iterate a json string using dictionary

706 views
Skip to first unread message

arnie

unread,
May 5, 2009, 12:30:04 PM5/5/09
to Google App Engine
I am getting a json string from an iphone application that is
consuming a google app engine application.
Can anybody let me know how to iterate over a json string using a
dictionary so that I can extract individual fields of the string
Thanks
Arnieterm

Pranav Prakash

unread,
May 5, 2009, 11:36:25 PM5/5/09
to Google App Engine
You need to use a JSON library for Python. Here is a good one that i
have found, and using for quite sometime.

http://code.google.com/p/google-app-engine-samples/source/browse/trunk/geochat/json.py

Also, refer to the Application's source code to understand how to use
this
http://code.google.com/p/google-app-engine-samples/source/browse/trunk/geochat/

Cheers,
--
Pranav Prakash

"This life is more than ordinary"

arnie

unread,
May 6, 2009, 12:10:02 AM5/6/09
to Google App Engine
I am getting a very simple json string that contains only 14 elements
Can't we use python dictionary to extract the elements of json?
Thanks
Arnie

风笑雪

unread,
May 6, 2009, 12:49:30 AM5/6/09
to google-a...@googlegroups.com
json = '{element1: "haha", element2: 123}'

d = eval(json)

print d['element1'], d.['element2']

2009/5/6 arnie <parv...@rediffmail.com>

djidjadji

unread,
May 6, 2009, 5:22:53 AM5/6/09
to google-a...@googlegroups.com
eval() the strings you receive in the request?
One way of getting to execute any python code in his application.

You can use the simple JSON that comes with GAE in Django

from django.utils import simplejson


2009/5/6 风笑雪 <kea...@gmail.com>:

ray

unread,
May 6, 2009, 9:35:38 AM5/6/09
to Google App Engine
Arnie,
I'm assuming you want to iterate over the json in Python?

Once you get the json string converted to a Python dictionary using
simplejson, you can use the dictionary's iteritems() method to iterate
over it in a python forloop like so:


from django.utils import simplejson

#jsonStr is your json string in Python
#convert to dict using simplejson:

jsonAsDict = simplejson.loads( jsonStr )
#jsonAsDict is now a python dict object

for i in jsonAsDict.iteritems():
i[0] #is the key
i[1] #is the value

Hope that helps.

Brandon

unread,
May 6, 2009, 10:09:45 AM5/6/09
to Google App Engine
I would also do it in this manner.
Reply all
Reply to author
Forward
0 new messages