Ios Mantle:忽略数组中嵌套字典中的键
我使用iOS中的框架构建一个简单的JSON结构,如下所示:Ios Mantle:忽略数组中嵌套字典中的键,ios,objective-c,json,parsing,mantle,Ios,Objective C,Json,Parsing,Mantle,我使用iOS中的框架构建一个简单的JSON结构,如下所示: { "posts": [ { "postId": "123", "title": "Travel plans", "location": "Europe" }, { "postId": "456", "title": "Vacation Photos",
{
"posts":
[
{
"postId": "123",
"title": "Travel plans",
"location": "Europe"
},
{
"postId": "456",
"title": "Vacation Photos",
"location": "Asia"
}
],
"updates": [
{
"friendId": "ABC123"
}
]
}
本质上,我只对“posts”
键感兴趣,希望完全忽略“updates”
键。此外,在“posts”
数组中,我希望完全忽略“location”
键。下面是我如何设置地幔模型的:
@interface MantlePost: MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSString *postId;
@property (nonatomic, strong) NSString *title;
@end
@implementation MantlePost
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"postId": @"postId",
@"title": @"title",
};
}
@end
最后,我将如何加载要转换的JSON:
- (NSDictionary *)loadJSONFromFile {
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"parse-response" ofType:@"json"];
NSError *error = nil;
NSData *jsonData = [[NSString stringWithContentsOfFile:jsonPath usedEncoding:nil error:&error] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
return jsonDict;
}
NSError = nil;
NSDictionary *jsonData = [self loadJSONFromFile];
MantlePosts *posts = (MantlePosts *)[MTLJSONAdapter modelOfClass:MantlePosts.class fromJSONDictionary:jsonData error:&error];
问题是,当我只显式映射postId
和title
时,我的MantlePosts
的后代数组包含所有3个属性postId
、title
。“updates”
数组被忽略,这正是我想要的,但我一直无法忽略后代数组中的某些键。在此方面的任何帮助都将不胜感激
下面是我在控制台中收到响应时收到的示例
(lldb) po posts
<MantlePosts: 0x6000000153c0> {
posts = (
{
location = Europe;
postId = 123;
title = "Travel plans";
},
{
location = Asia;
postId = 456;
title = "Vacation Photos";
}
);
}
(lldb)
(lldb)采购订单帖子
{
职位=(
{
地点=欧洲;
postId=123;
title=“旅行计划”;
},
{
地点=亚洲;
postId=456;
title=“假期照片”;
}
);
}
(lldb)
您所说的“壁炉架包含所有3个属性”是什么意思?您只声明了两个属性,如何访问第三个属性?这是我不理解的。JSON的所有属性都被映射,即使我在壁炉架上只指定了2个属性,你怎么知道它们被“映射”?您有没有任何示例代码可以说明这一点?我已经更新了显示控制台中的响应以及如何加载JSON文件的帖子。
(lldb) po posts
<MantlePosts: 0x6000000153c0> {
posts = (
{
location = Europe;
postId = 123;
title = "Travel plans";
},
{
location = Asia;
postId = 456;
title = "Vacation Photos";
}
);
}
(lldb)