On Sat, 2009-09-05 at 16:12 -0700, Eric wrote:
> Thank you for your input.  I tried what you suggested by first just
> trying to loop through the data like so:
> 
> "
> test_output = 0
> for obj in serializers.deserialize('json', gantt_data)['ganttgroups']:
>     test_output = test_output + 1
> "
> 
> This generated the following error:
> 
>      "'generator' object is unsubscriptable"
> 

Huh.  Okay.  I didn't realize deserialize would return a generator.

> I tried changing it slightly,  moving "['ganttgroups']" inside the
> brackets, to this:
> 
> "
> test_test_output = 0
> for obj in serializers.deserialize('json', gantt_data['ganttgroups']):
>     test_output = test_output + 1
> "
> 
> This generated the error I originally received:
> 
>     "string indices must be integers"
> 

Yes.  Now you're trying to subscript gantt_data before you deserialize
it.  As you know, gantt_data is a json string.  Hence the error.

> I am just so new to this that I'm not sure where to go from here. Do
> you have any other suggestions as to what I may be doing wrong?
> Again, any help with this would be greatly appreciated.
> 

Well, the first thing you need to do is figure out what you have to work
with.  You know now that deserializer returns a generator, so try
looping over it to see what it yields each time around: 

for obj in serializers.deserialize('json', gantt_data):
    print '---'
    print type(obj) 
    print obj

That should give you a hint how to proceed.  If you need more clues,
then add:

    print dir(obj)
    print help(obj)

That will give you some hints as to what you can do with obj.


> 
> 
> 
> On Sep 3, 1:09 pm, "J. Cliff Dyer" <j...@sdf.lonestar.org> wrote:
> > I suspect your error is hiding in <loop logic here>.  What do you expect
> > obj to be?  Your JSON should return a big dictionary with one key
> > ("ganttgroups").  When you iterate over a dictionary in python, you get
> > the keys of that dictionary.  In this case, the string "ganttgroups".
> > You may be doing the following:
> >
> > for obj in serializers.deserialize("json", gantt_data):
> >     do_something_to(obj['gantts'])
> >
> > which returns an error, because it evaluates to "ganttgroups"['gantts']
> > (which would give you the error you see.
> >
> > What you want is more like:
> >
> > for obj in serializers.deserialize('json', gantt_data)['ganttgroups']:
> >     start = obj['start']
> >     for gantt in obj['gantts']:
> >         for row in gantt['rows']:
> >             print row['own']
> >
> > In short, you're getting your dicts and lists mixed up, or your keys and
> > values.
> >
> > Cheers,
> > Cliff
> >
> > On Wed, 2009-09-02 at 10:40 -0700, Eric wrote:
> > > I forgot to mention that I am trying to deserialize the data as
> > > follows:
> >
> > > "
> > > ...
> > > gantt_data = request.POST.get('ganttdata')
> >
> > > for obj in serializers.deserialize("json", gantt_data):
> > >     <loop logic here>
> > > ...
> > > "
> >
> > > On Sep 2, 10:37 am, Eric <elezo...@gmail.com> wrote:
> > > > Hi,
> > > > I am attempting to parse a json string passed to my view via a form
> > > > post.   A simple example of my json structure is as follows (indented
> > > > for readability):
> >
> > > > {
> > > >     "ganttgroups":[
> > > >         {
> > > >             "gantts":[
> > > >                 {
> > > >                     "rows":[
> > > >                         {"stt":1, "end":2, "ttl":"test row - gr1 ga1
> > > > ta1",  "own":"Tim Johnson"},
> > > >                         {"stt":2, "end":3, "ttl":"my row (g1 t2)",
> > > > "own":"John Doe"},
> > > >                         {"stt":1, "end":2, "ttl":"test row - gr1 ga1
> > > > ta3", "own":"Mary Smith"}
> > > >                     ]
> > > >                 },
> > > >                 {
> > > >                     "rows":[
> > > >                         {"stt":1, "end":2, "ttl":"My 4th task",
> > > > "own":"Eric Johnson"},
> > > >                         {"stt":1, "end":2, "ttl":"my row (g2 t2)",
> > > > "own":"Jeff Smith"},
> > > >                         {"stt":1, "end":2, "ttl":"test row - gr1 ga2
> > > > t3", "own":"Bill Baker"}
> > > >                     ]
> > > >                 }
> > > >             ],
> > > >             "start":"2009-1-01"
> > > >         }
> > > >         ,{
> > > >             "gantts":[
> > > >                 {
> > > >                     "rows":[
> > > >                         {"stt":1, "end":2, "ttl":"row - gr2 ga1 t1",
> > > > "own":"Ted Tillman"},
> > > >                         {"stt":1, "end":2, "ttl":"row - gr2 ga1 t2",
> > > > "own":"Kim Crane"},
> > > >                         {"stt":1, "end":2, "ttl":"row - gr2 ga1 t3",
> > > > "own":"Bob Barker"}
> > > >                     ]
> > > >                 }
> > > >             ],
> > > >             "start":"2009-1-01"
> > > >         }
> > > >     ]
> >
> > > > }
> >
> > > > I would like to parse it so that I can loop over the pairs/arrays to
> > > > access the data. When I try to deserialize the data, I get the django
> > > > error "string indices must be integers". Can anybody please help me
> > > > determine what exactly this means and how I may fix this?  Is there
> > > > another method I should be using? I am obviously a bit of a newbie at
> > > > this so any help would be greatly appreciated.
> > 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to