-2

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

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
iOSDev
  • 3,617
  • 10
  • 51
  • 91
  • Any of your stock parsers will return the JSON parsed into an array/dictionary "nest". For Objective-C there are a half-dozen different "brands" of parsers, most free. You feed the JSON string in and it returns the "nest". – Hot Licks Jan 25 '13 at 01:40
  • Use an online JSON parser such as http://json.parser.online.fr/ to "visualize" the JSON. – Hot Licks Jan 25 '13 at 01:41
  • I would need help in parsing with SBJSon parser, pls help – iOSDev Jan 25 '13 at 01:46
  • Read the [online documentation](http://superloopy.io/json-framework/api/3.0/annotated.html). There is a method that converts string to object and another that converts object to string. You want the first. – Hot Licks Jan 25 '13 at 01:50
  • (Once again, the people voting to close were too lazy to find the dupes, so they used a bogus closing reason.) – Hot Licks Jan 25 '13 at 12:44

4 Answers4

2

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"];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • what is solution in this case ? i am also stuck for converting json to c# class object -{ 'aa-AA': { lanCODE: 'aa-AA', genNames: { female: ['Wavenet'], male: ['Bavenet', 'Bavenet'], }, default: 'Wavenet', systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'], name: 'xxxx', }, 'aa-AA': { lanCODE: 'aa-AA', genNames: { female: ['Wavenet'], male: ['Bavenet', 'Bavenet'], }, default: 'Wavenet', systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'], name: 'xxxx', } } – questp Apr 26 '21 at 14:06
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

Firo
  • 15,448
  • 3
  • 54
  • 74
1

Please read this tutorial step by step

Working with JSON in iOS

Please let me know still you get any trouble

swiftBoy
  • 35,607
  • 26
  • 136
  • 135