开发者

Trouble reading JSON object using Obj-C

开发者 https://www.devze.com 2023-02-09 15:35 出处:网络
I am trying to read the following json object using the json-framework and obj-C { Sections ={ Now = \"Wednesday 9 February 2011 02:40\";

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;
                    view开发者_Python百科sCounter = 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 !


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"];
       // …
    }
}


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"]);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号