0

I am trying to read the following json object using the json-framework and obj-C

{
Sections =     {
    Now = "Wednesday 9 February 2011 02:40";
    Section =         (
                    {
            Article =                 (
                                    {
                    Exceprt = "text here";
                    ID = 49011;
                    Title = "text here";
                    Type = Politics;
                    audioCounter = 0;
                    commentsCounter = 0;
                    hasMore = false;
                    important = False;
                    likesCounter = 0;
                    photoCounter = 0;
                    time = "21:12";
                    timeStamp = "2/8/2011 9:14:16 PM";
                    timeStatus = True;
                    videoCounter = 0;
                    viewsCounter = 0;
                },
                                    {
                    Exceprt = "text here";
                    ID = 49010;
                    Title = "text here";
                    Type = Politics;
                    audioCounter = 0;
                    commentsCounter = 0;
                    hasMore = false;
                    important = True;
                    likesCounter = 0;
                    photoCounter = 0;
                    time = "20:45";
                    timeStamp = "2/8/2011 9:10:59 PM";
                    timeStatus = True;
                    videoCounter = 0;
                    viewsCounter = 0;
                },
                                    {
                    Exceprt = "text here";
                    ID = 49008;
                    Title = "text here";
                    Type = Politics;
                    audioCounter = 0;
                    commentsCounter = 0;
                    hasMore = false;
                    important = False;
                    likesCounter = 0;
                    photoCounter = 0;
                    time = "20:28";
                    timeStamp = "2/8/2011 9:09:44 PM";
                    timeStatus = True;
                    videoCounter = 0;
                    viewsCounter = 0;
                }
            );
            ID = 22;
            Name = "EN Live";
            totalNews = 3416;
        }
    );
};
}

My intent is to have a list of the articles (list of dictionaries) so that I can later access them easily. I have been stuck a while on this and my code is giving me an error about calling a non existent method for NSArray which has led me to suspect that I am misunderstanding the json object. I am totally new to this and any help is greatly appreciated.

Here's my code:

    NSDictionary *results = [jsonString JSONValue];
NSDictionary *Articles = [[results objectForKey:@"Sections"]    objectForKey:@"Section"];
NSArray *ListOfArticles = [Articles objectForKey:@"Article"];

for (NSDictionary *article in ListOfArticles)
{

    NSString *title = [article objectForKey:@"Title"];
    NSLog(title);
}

Thanks !

2
  • One note: Exceprt is misspelt. The correct spelling is Excerpt. Commented Feb 9, 2011 at 1:11
  • Good eye ! Unfortunately I have no control over that :) Commented Feb 9, 2011 at 1:15

2 Answers 2

1

First of all, those aren’t valid JSON data. Names (in name/value pairs) are strings and must be quoted. String values must always be quoted. Boolean values must be either true or false (lowercase). Check http://json.org/ and http://www.ietf.org/rfc/rfc4627.txt?number=4627 and http://jsonlint.com

Here’s the structure of your data:

  1. The top level value is an object (dictionary)
  2. This object has a name (key) called Sections whose value is itself another object (dictionary)
  3. Sections has a name (key) called Section whose value is an array
  4. Each element in the Section array is an object (dictionary)
  5. Each element in the Section array has a name (key) called Article whose value is an array, as well as other names (keys): ID, title, totalNews
  6. Each element in the Article array is an object

If your JSON data were valid, you could parse them as follows:

// 1.
NSDictionary *results = [jsonString JSONValue];
// 2.
NSDictionary *sections = [results objectForKey:@"Sections"];
// 3.
NSArray *sectionsArray = [sections objectForKey:@"Section"];
// 4.
for (NSDictionary *section in sectionsArray) {
    // 5.
    NSLog(@"Section ID = %@", [section objectForKey:@"ID"];
    NSLog(@"Section Title = %@", [section objectForKey:@"Title"];
    NSArray *articles = [section objectForKey:@"Article"];
    // 6.
    for (NSDictionary *article in articles) {
        NSLog(@"Article ID = %@", [article objectForKey:@"ID"];
        NSLog(@"Article Title = %@", [article objectForKey:@"Title"];
       // …
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer! I used an online viewing tool and managed to notice that the data is indeed weird. Unfortunately, I have no control over it and I just wrote a couple of lines to deal with the id, names, total news at the end. Thank you again !
Ouch! I’m not sure if there’s an Objective-C JSON parser that is tolerant to invalid data. Good luck!
0

Your JSON framework is probably parsing out an NSDictionary where you're expecting an NSArray. It'll let you assign an NSDictionary to an NSArray, but then you'll get a runtime exception when you attempt to call a method on your "array". Judging by the JSON you posted (which isn't correct JSON), this is what I would have my parsing code look like. The names of the NSDictionaries and NSArrays are simply named after the JSON attributes they represent.

NSDictionary* results = [jsonString JSONValue];
NSDictionary* sections = [results valueForKey:@"Sections"];
NSArray* section = [sections valueForKey:@"Section"];
NSArray article = [[section objectAtIndex:0] valueForKey:@"Article"];

for (NSDictionary* anArticle in article) {
    NSLog(@"%@", [anArticle valueForKey:@"Title"]);
}

3 Comments

One remark: -valueForKey: is a Key-Value Coding method and as such it has different semantics and behaviour as compared to -objectForKey:, which is the canonical NSDictionary method to obtain the objects stored in a dictionary. See stackoverflow.com/questions/4489684/…
Cheers for the answer mate ! The json is indeed malformed but I don't have any control over that :(
@Bavarious, true enough. Bad muscle memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.