解析复杂的JSON数据

时间:2013-01-25 01:36:11

标签: iphone ios objective-c sbjson

我想以下列形式解析来自服务器的复杂json数据:

    {

    "Data 1":{
        "2012-12-01":[
            {
                "field 1":"field 2",
                "summary":"summary 1"
            },
            {
                "field":"field 2",
                "summary":"summary 2"
            },
            {
                "service":"service 3",
                "summary":"summary 3"
            }
        ],
        "2012-12-10":[
            {
                "field":"field 1",
                "summary":"summary 1"
            },
            {
                "field":"field 2",
                "summary":"summary 2"
            }
        ],
        "2012-12-31":[
            {
                "field":"field 1",
                "summary":"summary 1"
            }
        ]
    },
    "Data 2":{
        "2013-01-4":[
            {
                "field":"field 1",
                "summary":"summary 1"
            },
            {
                "field":"field 2",
                "summary":"summary 2"
            },
            {
                "field":"field 3",
                "summary":"summary 3"
            }
        ],
        "2013-01-8":[
            {
                "field":"field 1",
                "summary":"summary 1"
            },
            {
                "field":"field 2",
                "summary":"summary 2"
            }
        ],
        "2013-01-25":[
            {
                "field":"field 1",
                "summary":"summary 1"
            }
        ]
    },
    "Data 3":{
        "2013-02-09":[
            {
                "field":"field 1",
                "summary":"summary 1"
            },
            {
                "field":"field 2",
                "summary":"summary 2"
            },
            {
                "field":"field 3",
                "summary":"summary 3"
            }
        ]
    }

} 

如何使用SBJSON解析它?

请帮忙

提前致谢

4 个答案:

答案 0 :(得分:2)

解析JSON是一行命令。如果你想使用iOS 5及更高版本中内置的JSON解析器,NSJSONSerialization它就是:

NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                           options:0
                                                             error:&error];

如果你想使用SBJSON,那就是:

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dictionary = [parser objectWithData:data];

无论如何,既然您已经在NSDictionary中获得了JSON,那么您可以继续进行适当的导航,例如:

NSDictionary *data1 = [dictionary objectForKey:@"Data 1"];
NSArray *arrayDecFirst2012 = [data1 objectForKey:@"2012-12-01"];
NSDictionary *firstDictionaryInDecFirst2012 = [arrayDecFirst2012 objectAtIndex:0];
NSString *fieldOne = [firstDictionaryInDecFirst2012 objectForKey:@"field 1"];

或者,如果您想使用新的下标符号,那就是:

NSDictionary *data1 = dictionary[@"Data 1"];
NSArray *arrayDecFirst2012 = data1[@"2012-12-01"];
NSDictionary *firstDictionaryInDecFirst2012 = arrayDecFirst2012[0];
NSString *fieldOne = firstDictionaryInDecFirst2012[@"field 1"];

答案 1 :(得分:1)

当“漂亮印刷”时,你的JSON的第一部分看起来像这样 -

{
    "Data 1":{
        "2012-12-01":[
            {
                "field 1":"field 2",
                "summary":"summary 1"
            },
            {
                "field":"field 2",
                "summary":"summary 2"
            },
            {
                "service":"service 3",
                "summary":"summary 3"
            }
        ],
        "2012-12-10":[
            {
                "field":"field 1",
                "summary":"summary 1"
            },
            {
                "field":"field 2",
                "summary":"summary 2"
            }
        ],
        "2012-12-31":[
            {
                "field":"field 1",
                "summary":"summary 1"
            }
        ]
    },
    "Data 2":{
        "2013-01-4":[

JSON的最外层部分是一个字典(JSON称之为“对象”) - 您可以通过打开{来判断。在该字典中是一个称为“数据1”的键/对象。该对象是一个字典(另一个{),其第一个键/对象称为“2012-12-01”。第二对的对象是包含三个字典的数组([)。等

答案 2 :(得分:1)

我从来没有专门使用过SBJSON,但从它的外观来看,你应该能够做到:

(假设您的JSON当前是NSString)

NSDictionary *arrayOfJSONString = [SBJsonObject objectWithString:jsonString];

或类似的东西。

我个人有时会使用JSONKit。它很小,只需一个.h / .m就可以包含在你的项目中。 (它也包含在RestKit中)。在这种情况下,您只需:

NSDictionary *jsonDict = [jsonString objectFromJSONString];

然后你就可以使用它:

NSDictionary *dataOne = [jsonDict objectForKey:@"Data 1"];

如果你想创建Objc-C课程,我会看看RestKit

答案 3 :(得分:1)

请逐步阅读本教程

Working with JSON in iOS

请让我知道你仍然遇到麻烦