Вопрос

I want to parse complex json data coming from server in following form:

    {

    "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"
            }
        ]
    }

} 

How do I parse it using SBJSON?

Please help

Thanks in advance

Это было полезно?

Решение 2

The first part of your JSON looks something like this when "pretty printed" --

{
    "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":[

The outermost part of the JSON is a dictionary (JSON calls it an "object") -- you can tell because of the opening {. In that dictionary is a key/object pair called "Data 1". The object is a dictionary (another {) whose first key/object pair is called "2012-12-01". The object of this second pair is an array (the [) containing three dictionaries. Etc.

Другие советы

To parse the JSON is a one line command. If you want to use the JSON parser built into iOS 5 and later, NSJSONSerialization it's just:

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

If you want to use SBJSON, it's:

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

Anyway, now that you've got your JSON in a NSDictionary, you can go ahead and navigate it appropriately, e.g.:

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

Or, if you want to use the new subscripting notation, that would be:

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

I have never used SBJSON specifically but by the looks of it you should be able to just do:

(this is assuming your JSON is currently an NSString)

NSDictionary *arrayOfJSONString = [SBJsonObject objectWithString:jsonString];

or something similar to that.

I personally will use JSONKit sometimes. It is small, just a .h/.m to include in your project. (It is also included in RestKit). In this case you would just do:

NSDictionary *jsonDict = [jsonString objectFromJSONString];

Then you can use it like:

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

If you would like to create Objc-C classes instead I would take a look at RestKit

Please read this tutorial step by step

Working with JSON in iOS

Please let me know still you get any trouble

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top