如何使用NSDictionary收集嵌套在数组中的JSON数据 [英] How do I gather JSON data nested in an array using NSDictionary

查看:118
本文介绍了如何使用NSDictionary收集嵌套在数组中的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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"
                }

    ]
}

以下代码

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);
    }

我最终如何记录current_campaign标题?
我试过

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

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

并没有成功。我在Objective C上相当新,并且无法理解你如何使用NSArray和NSDictionary挖掘JSON。任何帮助将不胜感激!

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!

推荐答案

关于 JSON 最容易记住的是每次你看一个括号[,表示数组的开头和]结束。每当你看到一个大括号{表示字典的开头和}结束时。

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.

因此,在您的示例中,广告系列是一个字典元素,另一个字典 current_campaigns ),其中包含数组 词典。每个词典都有一个,名为 title

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

这篇关于如何使用NSDictionary收集嵌套在数组中的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆