Pierre
Pierre

Reputation: 11633

Parse SBJSON + Syntax

I would lie to parse a JSON file with SBJSON but I've a little problem of comprehension ? Here is my JSON structure :

So for exemple :

[{"id":"1","text":"blabla","date":"1235432241"},{"id":"2","text":"blabla2","date":"1235432241"}]

And here's the code I found to parse a JSON file whit SBJSON :


- (void)downloadJSONFile:(NSData *)data 
{   
    SBJSON *jsonParser = [[[SBJSON alloc] init] autorelease];
    NSString *jsonString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

    id response = [jsonParser objectWithString:jsonString error:NULL];

    NSDictionary *feed = (NSDictionary *)response;
    NSArray *nodes = (NSArray *)[feed valueForKey:@"keyValue"]; // here's I don't know what to put

    for (int i = 0; i nodes.count; i++) {

        NSDictionary *node = (NSDictionary *)[nodes objectAtIndex:i];
        NSLog(@"id = %@",[node objectForKey:@"id"]);
    }
}

I don't know what to write in the valueforKey of my nodes array ... Someone can explain to me ? Thanks !

Upvotes: 0

Views: 1684

Answers (2)

Robert Höglund
Robert Höglund

Reputation: 10070

By the look of your supplied json sample you should get an NSArray containing two NSDictionary. So the code should be

NSArray *nodes = (NSArray *)response;

Not being familiar with SBJSON it is possible that response actually is an NSDictionary. In that case you can just print the dictionary using NSLog(@"%@", feed); You should get a print out that looks something like:

{
  nameOfKey =  (
              {
               "id" = 1;
               "text" = "blablabla"
...

Where nameOfKey would be the keyValue you are looking for.

Upvotes: 0

Bourne
Bourne

Reputation: 10302

Your keys are: id, text and date. So if you wish to parse the objects with the key id, then in that line which you've highlighted it would be :

valueForKey:@"id"

Upvotes: 1

Related Questions