2013-10-29 103 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"];