Ios 如何在json中检索键的特定值?
这是我的json内容Ios 如何在json中检索键的特定值?,ios,objective-c,json,Ios,Objective C,Json,这是我的json内容 [ { "sha":"30eae8a47d0203ac81699d8fc2ab2632de2d0bba", "commit":{ "author":{ "name":"Madhura Bhave", "email":"mbhave@pivotal.io", "date":"2017-03-23T23:14:32Z" }, "committer":{
[
{
"sha":"30eae8a47d0203ac81699d8fc2ab2632de2d0bba",
"commit":{
"author":{
"name":"Madhura Bhave",
"email":"mbhave@pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"committer":{
"name":"Madhura Bhave",
"email":"mbhave@pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"message":"Merge branch '1.5.x'",
}
}
]
这是我的主要任务。我只想从邮件中检索键值,从提交者字典中检索姓名、电子邮件、日期。我被困在了如何做到这一点上
NSMutableArray *CommitArray = [[NSMutableArray alloc] init];
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
commitDictObj.message = [CommitDictionary objectForKey:@"message"];
for (NSDictionary *CommitterDictionary in [CommitDictionary objectForKey:@"committer"]) {
Committer *author = [[Committer alloc] init];
author.name = [CommitterDictionary objectForKey:@"name"];
author.email = [CommitterDictionary objectForKey:@"email"];
author.date = [CommitterDictionary objectForKey:@"date"];
}
[CommitArray addObject:commitDictObj];
}
for (int i =0 ; i < [CommitArray count] ; i++){
CommitDict *commitDictObj = [CommitArray objectAtIndex:i];
NSLog(@"Commit Message: %@", commitDictObj.message);
}
return 0;
}
}
NSMutableArray*CommitArray=[[NSMutableArray alloc]init];
for(CommitJson中的NSDictionary*CommitDictionary){
CommitDict*commitDictObj=[[CommitDict alloc]init];
committedcotbj.message=[committedictionary objectForKey:@“message”];
对于(在[CommitDictionary objectForKey:@“committer”]中的NSDictionary*CommitterDictionary){
提交人*author=[[Committer alloc]init];
author.name=[CommitterDictionary objectForKey:@“name”];
author.email=[CommitterDictionary objectForKey:@“email”];
author.date=[CommitterDictionary objectForKey:@“date”];
}
[CommitArray addObject:commitDictObj];
}
对于(int i=0;i<[CommitArray count];i++){
CommitDisct*CommitDisctobj=[CommitArray对象索引:i];
NSLog(@“提交消息:%@”,committedcotbj.Message);
}
返回0;
}
}
我尝试获取json并显示它的message、name、email和date值。我如何记录message、name、email和date的值?您的数组包含一个字典,而该字典包含
commit
字典,而不是直接包含commit
字典。替换代码的该部分:
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
因此:
for (NSDictionary *shaCommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
NSDictionary *CommitDictionary = [shaCommitDictionary objectForKey:@"commit"];
(1) 将JSON转换为NSDictionary
NSData *jsonData= ... // Assume you got the data already loaded
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
(2) 访问字典值(快速枚举现在可用
NSString *message = dictionary[@"message"];
NSDictionary *author = dictionary[@"author"];
NSString *name = author[@"author"];
NSString *email = author[@"author"];
NSString *date = author[@"author"];
// OR:
// NSString *name = dictionary[@"author"][@"author"];
// NSString *email = dictionary[@"author"][@"author"];
// NSString *date = dictionary[@"author"][@"author"];
就这样。我认为棘手的事情是把JSON数据放到NSDictionary中?
请参见此处:打印提交数组一次,打印提交JSON一次,先生..我不明白..您能解释更多细节吗?您需要澄清您的问题。请回答您的问题并提供更多细节。您发布的代码究竟以什么方式不起作用?它编译了吗?它崩溃了吗?它能走多远?打印一次此数据id提交JSON sample包含不平衡的[]和{};您的代码使用的键“commits_url”在您显示的JSON中没有出现;并且您不清楚您的代码哪里出错了。如果人们想帮助您而不进行大量猜测,您需要澄清您的问题并提供更多细节。