How do I gather JSON data nested in an array using NSDictionary

前端 未结 1 1124
心在旅途
心在旅途 2020-12-22 06:55

I have the following JSON:

{
0: 200,
error: false,
campaigns: {
current_campaigns: [
                {
                id: \"1150\",
                campaign         


        
相关标签:
1条回答
  • 2020-12-22 07:06

    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"]);
    } 
    
    0 讨论(0)
提交回复
热议问题