0

I have the following JSON:

{
0: 200,
error: false,
campaigns: {
current_campaigns: [
                {
                id: "1150",
                campaign_type_id: "1",
                campaign_type: "Type",
                title: "Name (with type) ",
                url: "http://www.example.com",
                special: null,
                campaign_instructions: "Here's what you do",
                pay_description: "",
                start: "2013-10-14 00:00:00",
                end: "2014-03-31 23:59:59"
                },
                {
                id: "1151",
                campaign_type_id: "1",
                campaign_type: "Type",
                title: "Name (with type) ",
                url: "http://www.example.com",
                special: null,
                campaign_instructions: "Here's what you do",
                pay_description: "",
                start: "2013-10-14 00:00:00",
                end: "2014-03-31 23:59:59"
                },
    ],
    new_campaigns: [
                {
                id: "1152",
                campaign_type_id: "1",
                campaign_type: "Type",
                title: "Name (with type) ",
                url: "http://www.example.com",
                special: null,
                campaign_instructions: "Here's what you do",
                pay_description: "",
                start: "2013-10-14 00:00:00",
                end: "2014-03-31 23:59:59"
                }

    ]
}

And the following code

NSURL *theJSON = [NSURL URLWithString:@"http://somejsonurl"];
NSURLRequest *request = [NSURLRequest requestWithURL:theJSON];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
    NSError *errorJson = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0     error:&errorJson];

    NSArray *campaigns = dataDictionary[@"campaigns"];

    for (NSDictionary *campaignList in campaigns) {
        NSLog(@"Call gave: %@", campaigns);
    }

How would I end up logging the current_campaign title? I tried

   NSLog(@"%@", [campaignList objectForKey:@"title"]);
   NSLog(@"%@", campaigns[@"title"] );

and no success. I'm fairly new at Objective C and am having trouble understanding how you dig into JSON with NSArray and NSDictionary. Any help would be greatly appreciated!

Aaron
  • 7,055
  • 2
  • 38
  • 53
Aubrey
  • 629
  • 1
  • 7
  • 17
  • 1
    Go to json.org and learn the JSON syntax. It takes only 5-10 minutes. – Hot Licks May 12 '14 at 01:47
  • "Go learn X" is probably the least helpful way to assist someone with a question. – Aubrey May 28 '14 at 00:33
  • **DID YOU TRY???** It really **does** only take only 5-10 minutes to learn, and when you learn it JSON becomes far less confusing. It's really blazingly simple when you understand it. – Hot Licks May 28 '14 at 01:12
  • I agree with Hot Licks. My tip on recognizing [] and {} will help you understand the breakdown of the two structures that make up JSON objects, but this is just the beginning. I would also suggest learning more about it. I think Hot Licks is just trying to help. – LJ Wilson May 28 '14 at 17:38
  • I think your approach is different ElJay. Helpful vs http://bit.ly/1nGQMVA – Aubrey May 29 '14 at 16:24

1 Answers1

3

The easiest thing to remember about JSON is that every time you see a bracket "[", that means the beginning of an Array and "]" is the end. Every time you see a curly brace "{" that means the beginning of a Dictionary and "}" is the end.

So in your example, campaigns is a Dictionary element with another Dictionary (current_campaigns) that contains an Array of Dictionaries. Each of those Dictionaries has a key called title.

So the long version (untested):

NSDictionary *campaigns = [dataDictionary objectForKey:@"campaigns"];
NSArray *currentCampaigns = [campaigns objectForKey:@"current_campaigns"];
for (NSDictionary *thisCampaign in currentCampaigns) {
    NSLog(@"title: %@", [thisCampaign objectForKey:@"title"]);
} 
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • 2
    Not only did this work (syntax was missing ':' after objectForKey but nbd) like magic... your hint on [] and {} was exactly the tip I needed to do it right going forward. Thank you! – Aubrey Mar 08 '14 at 07:52
  • 1
    @Aubrey -- That's odd because that was the exact advice I gave you -- to go to json.org and learn the syntax. Why was that "not helpful" when the "hint" you got above was the exact same thing?? – Hot Licks May 28 '14 at 15:44
  • The advice you gave was "go learn it somewhere" vs offering a solution as ElJay did above. Amazing how you have this level of cred when you troll like this. – Aubrey May 29 '14 at 16:24