2013-10-29 71 views
0

嗯,我想它是一个简单的问题(因为我只是最近才学习IOS)。IOS JSON解析嵌套数据

我看过的大多数教程都显示简单的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"]; 

然后*名称应评估:ZITA。

但我的问题是....

1)我需要什么巫术,以获得访问数据低沉,像结构里面做“水平=国家;” (该级别位于位于“位置”内部的“地理”内)

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

+0

只是继续做更多的相同。 –

回答

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