IOS JSON解析嵌套数据
Posted
技术标签:
【中文标题】IOS JSON解析嵌套数据【英文标题】:IOS JSON Parsing Nested Data 【发布时间】:2013-10-29 21:13:08 【问题描述】:嗯,我想这是一个简单的问题(因为我最近才学习 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) 我需要做什么才能访问结构深处的数据,例如“level = country;” (关卡位于“位置”内的“地理”内)
有人能解释一下怎么做吗?
【问题讨论】:
继续做更多相同的事情。 【参考方案1】:和你现在做的完全一样:)
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"];
【讨论】:
以上是关于IOS JSON解析嵌套数据的主要内容,如果未能解决你的问题,请参考以下文章