-2

我想以以下形式解析来自服务器的复杂 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

4 回答 4

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];

无论如何,既然您已经在 a 中获得了 JSON NSDictionary,您可以继续并适当地导航它,例如:

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"];
于 2013-01-25T03:00:58.317 回答
1

请逐步阅读本教程

在 iOS 中使用 JSON

请让我知道你还有什么麻烦

于 2013-01-25T03:33:19.740 回答
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

于 2013-01-25T02:56:53.667 回答
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 称它为“对象”)——你可以从{. 在该字典中有一个名为“Data 1”的键/对象对。该对象是一个字典(另一个{),其第一个键/对象对称为“2012-12-01”。第二对的对象是一个[包含三个字典的数组 (the )。等等。

于 2013-01-25T01:47:31.553 回答