我试图获得使用githubs地幔的JSON文件的子节点。这就是我试过的:
JSON
"countries": {
"name": "germany",
"population": "80620000000",
"populationInCities": {
"Hamburg": 1799000,
"Berlin": 3502000,
"Munich": 1378000
}
}CountryInfo.h Info.h
#import <Mantle/Mantle.h>
@interface CountryInfo : MTLModel <MTLJSONSerializing>
@property (nonatomic, readonly, copy) NSString *collectionName;
@property (nonatomic, readonly, assign) NSUInteger cPopulation;
@property (nonatomic, readonly, copy) NSArray *populationInCities;
@endCountryInfo.m Info.m
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{ @"cName": @"name",
@"cPopulation": @"population",
@"populationInCities": [NSSet setWithArray:@[ @"Hamburg", @"Hamburg", @"Hamburg"]]
};
}
+ (NSValueTransformer *)populationInCitiesJSONTransformer {
return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CountryInfo.class];
}当我运行我的应用程序时,我收到了一个错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'populationInCities must either map to a JSON key path or NSNull, got: {
populationInCities = (
Hamburg,
Berlin,
Munich
);
}.'发布于 2014-12-26 19:30:49
如果要在populationInCities数组中存储总体数字,则需要自定义转换器:
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{@"collectionName": @"name",
@"cPopulation": @"population"};// 'populationInCities' names in JSON and model are the same, so no need to include here.
}
+ (NSValueTransformer *)populationInCitiesJSONTransformer {
return [MTLValueTransformer transformerWithBlock:^(NSDictionary *dict) {
return [dict allValues];
}];
}发布于 2015-01-11 21:38:15
很尴尬,但它很简单地用了。-符号。
例如popukationInCities.hamburg
https://stackoverflow.com/questions/27045220
复制相似问题