tags:

views:

39

answers:

1

Hello,

I'm quite new to iphone programming and I want to do the following stuff:

  1. get data from a JSON REST web server
  2. parse the received data using YAJL
  3. Draw a graph with those data using core-plot

So, 1th item is fine, I use ASIHttpRequest which runs as espected 3rd is almost fine (I still have to learn how to tune core-plot).

The problem I have is regarding 2nd item. I use YAJL as it seems to be the faster parser, so why not give it a try :)

Here is the part of code that gets the data from the server and parse them:

// Get server data
response_data = [request responseData];

// Parse JSON received
self.arrayFromData = [response_data yajl_JSON];
NSLog(@"Array from data: %@", self.arrayFromData);

The parsing works quite well in fact, the NSLog output is something like:

2010-06-14 17:56:35.375 TEST_APP[3733:207] Array from data :

{
data =     (
            {
        val = 1317;
        date = "2010-06-10T15:50:01+02:00";
    },
            {
        val = 1573;
        date = "2010-06-10T16:20:01+02:00";
    },
        ........
    {
        val = 840;
        date = "2010-06-11T14:50:01+02:00";
    },
            {
        val = 1265;
        date = "2010-06-11T15:20:01+02:00";
    }
);
from = "2010-06-10T15:50:01+02:00";
to = "2010-06-11T15:20:01+02:00";    
max = "2590";    
}

According to th yajl-objc explanations http://github.com/gabriel/yajl-objc, the parsing returns a NSArray... The thing is... I do not know how to get all the values from it as for me it looks more like a NSDictionary than a NSArray...

Could you please help ?

Thanks a lot, Luc

edit1: it happens that this object is actually a NSCFDictionary (!), I am still not able to get value from it, when I try the objectFromKey method (that should work on a Dictionary, no ?) it fails.

+1  A: 

It's returning an NSDictionary. NSCFDictionary is a private subclass and is immaterial to this discussion. So it looks like you'd retrieve stuff like:

NSDictionary * responseDictionary = ...;
NSArray * dataArray = [responseDictionary objectForKey:@"data"];
for (NSDictionary * dataPair in dataArray) {
  NSLog(@"val: %@, date: %@", [dataPair objectForKey:@"val"], [dataPair objectForKey:@"date"]);
}
Dave DeLong
Thanks Dave, I've just figured out why the objectForKey method was not working (I was not using the correct key, silly me ....). The thing is, I still do not understand why it's a NSDictionary when it is supposed to be a NSArray (as specified in the doc).Thanks a lot.
Luc