0

好吧,我想这是一个简单的问题(因为我最近才学习 IOS)。

我见过的大多数教程都展示了简单的 JSON 键值示例。但是,我正在寻找具有以下格式的 JSON 结构:

因此,我可以说一个显示如下内容的 JSON 页面:

loans: (
    {
    activity = "Personal Products Sales";
    "basket_amount" = 0;
    "bonus_credit_eligibility" = 1;
    "borrower_count" = 1;
    description =         {
        languages =             (
            en
        );
    };
    "funded_amount" = 0;
    id = 623727;
    image =         {
        id = 1457061;
        "template_id" = 1;
    };
    "loan_amount" = 475;
    location =         {
        country = Philippines;
        "country_code" = PH;
        geo =             {
            level = country;
            pairs = "13 122";
            type = point;
        };
        town = "Maasin City, Leyte";
    };
    name = Zita;
    "partner_id" = 145;
    "planned_expiration_date" = "2013-11-28T21:00:02Z";
    "posted_date" = "2013-10-29T21:00:02Z";
    sector = Retail;
    status = fundraising;
    use = "to buy additional stocks of soap, toothpaste, dish washing products, etc.";
},

因此,例如,如果我想提取名称,我了解密钥对的想法,所以我只需执行以下操作:

 NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1

                      options:kNilOptions
                      error:&error];

NSArray* latestLoans = [json objectForKey:@"loans"]; //2
 NSDictionary* loan = [latestLoans objectAtIndex:0];
NSString *name = [loan objectForKey:@"name"];

然后 *name 应该评估为:Zita。

但我的问题是......

1)我需要做什么才能获得结构深处的访问数据,如“级别=国家;” (关卡位于“位置”内的“地理”内)

有人可以解释如何做到这一点吗?

4

1 回答 1

3

和你现在做的完全一样:)

NSDictionary* loan = [latestLoans objectAtIndex:0];
NSDictionary* location = [loan objectForKey:@"location"];
NSDictionary* geo = [locationobjectForKey:@"geo"];
NSString* level = [geo objectforKey:@"country"];

或更短:

NSDictionary* loan = [latestLoans objectAtIndex:0];
NSString* level = loan[@"location"][@"geo"][@"level"];
于 2013-10-29T21:21:04.170 回答