[英]Nested NSDictionary from JSON in Xcode
嗨,我遇到了巨大的问题,无法为我提供一些非常复杂的JSON结构数据。
问题在于JSON结构包含一些我无法找到如何处理的嵌套。
我从数据库中获取的JSON示例:
{
"hits": {
"totalHits": 3202,
"hits": [{
"id": "70132eb7-2834-458c-900a-da951c95a506",
"versions": [{
"id": 7,
"properties": {
"Status": [
"usable"
],
"created": [
"2015-10-27T14:31:13Z"
],
"Text": [
"Snabbtåg, Järnväg, Höghastighetsjärnväg, "
],
"ConceptDefinitionLong": [
"Enligt Trafikverket saknas en helt entydig och vedertagen definition av höghastigheteståg."
],
"contenttype": [
"Concept"
],
"ConceptProposalUser": [
"[object Object]"
],
"ConceptType": [
"object"
],
"Name": [
"Höghastighetståg"
],
"ConceptNote": null,
"ConceptSeeAlso": null,
"Note": null,
"ConceptName": [
"Höghastighetståg"
],
"updated": [
"2016-02-01T11:37:30Z"
],
"ConceptDefinitionShort": [
"Snabbtåg, Järnväg, Höghastighetsjärnväg, Kollektivtrafik"
],
"ConceptStatus": [
"usable"
]
}
}],
"noVersions": 1
}, {
"id": "4224ccfb-1f0a-9491-727f-f6ab0fc2c951",
"versions": [{
"id": 2,
"properties": {
"Status": [
"usable"
],
"created": [
"2016-01-25T12:03:33Z"
],
"Text": [
"Rosenlundsbadet öppnade 1968 och äventyrsbadet 1991."
],
"ConceptDefinitionLong": [
"Rosenlundsbadet är en badanläggning i Jönköping. "
],
"contenttype": [
"Concept"
],
"ConceptProposalUser": null,
"ConceptType": [
"organisation"
],
"Name": [
"Rosenlundsbadet"
],
"ConceptNote": null,
"ConceptSeeAlso": null,
"Note": null,
"ConceptName": [
"Rosenlundsbadet"
],
"updated": [
"2016-01-25T12:03:38Z"
],
"ConceptDefinitionShort": [
"Simning, Simhopp, Äventyrsbad"
],
"ConceptStatus": [
"usable"
]
}
}],
"noVersions": 1
}
...
我的工作是从“状态”设置为“可用”的所有帖子中获取所有“名称”。 并将它们存储在我的应用程序中。 但是我很难获取这些信息。 我是JSON的新手,无法处理该结构。 这是我经过数小时的谷歌搜索后得到的:
NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:@"http://www.pumba.se/example.json"]];
NSError *error;
NSMutableDictionary *JSONdictionary = [NSJSONSerialization
JSONObjectWithData:allCoursesData
options:kNilOptions
error:&error];
if( error )
{
NSLog(@"%@", [error localizedDescription]);
}
else {
NSArray* entries = [JSONdictionary valueForKeyPath:@"hits.hits"];
NSDictionary *firstItem = [entries objectAtIndex:0];
NSString *id1 = [firstItem objectForKey:@"id"];
NSLog(@"ID: ");
NSLog(id1);
}}
从该代码中,我可以从第一项中获取ID。 但是我似乎无法获取“名称”或检查它们是否“可用”。 一直在处理这个问题,并尝试解决了几个小时,但这超出了我的能力。 我想为什么我不能解决它的原因是因为数据嵌套在hits.hits等中。但是我必须解决这个问题才能完成我的应用程序。 我将非常感谢您的帮助。
这是JSON数据库的较长版本。 完整版本包含3000多个项目: http : //jsonviewer.stack.hu/#http : //www.pumba.se/example.json
您可以使用以下代码:
NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:@"http://www.pumba.se/example.json"]];
NSError *error;
NSMutableDictionary *JSONdictionary = [NSJSONSerialization
JSONObjectWithData:allCoursesData
options:kNilOptions
error:&error];
if( error )
{
NSLog(@"%@", [error localizedDescription]);
}
else {
NSMutableArray *allNames = [NSMutableArray array];
NSArray* entries = [JSONdictionary valueForKeyPath:@"hits.hits"];
for (NSDictionary *hit in entries) {
NSArray *versions = hit[@"versions"];
for (NSDictionary *version in versions) {
NSDictionary *properties = version[@"properties"];
NSString *status = [properties[@"Status"] firstObject];
NSString *name = [properties[@"Name"] firstObject];
if ([status isEqualToString:@"usable"]) {
[allNames addObject:name];
}
}
}
NSLog(@"All names: %@", allNames);
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.