【发布时间】:2014-03-08 02:34:10
【问题描述】:
我有以下 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 标题? 我试过了
NSLog(@"%@", [campaignList objectForKey:@"title"]);
NSLog(@"%@", campaigns[@"title"] );
并没有成功。我是 Objective C 的新手,无法理解如何使用 NSArray 和 NSDictionary 深入研究 JSON。任何帮助将不胜感激!
【问题讨论】:
-
访问 json.org 并学习 JSON 语法。只需 5-10 分钟。
-
“去学习 X”可能是帮助某人解决问题的最没有帮助的方式。
-
你试过了吗???真的确实只需要 5-10 分钟来学习,当你学会它时,JSON 变得少得多令人困惑。当你理解它时,它真的非常简单。
-
我同意 Hot Licks。我关于识别 [] 和 {} 的提示将帮助您理解构成 JSON 对象的两种结构的分解,但这仅仅是开始。我还建议更多地了解它。我认为 Hot Licks 只是想提供帮助。
-
我认为你的方法与 ElJay 不同。有帮助 vs bit.ly/1nGQMVA
标签: ios objective-c json