首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用github-地幔获取JSON的subTree

利用github-地幔获取JSON的subTree
EN

Stack Overflow用户
提问于 2014-11-20 16:56:42
回答 2查看 223关注 0票数 0

我试图获得使用githubs地幔的JSON文件的子节点。这就是我试过的:

JSON

代码语言:javascript
复制
"countries": {
        "name": "germany",
        "population": "80620000000",
        "populationInCities": {
            "Hamburg": 1799000,
            "Berlin": 3502000,
            "Munich": 1378000
        }
    }

CountryInfo.h Info.h

代码语言:javascript
复制
#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;

@end

CountryInfo.m Info.m

代码语言:javascript
复制
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
        return @{ @"cName": @"name",
                  @"cPopulation": @"population",
                  @"populationInCities": [NSSet setWithArray:@[ @"Hamburg", @"Hamburg", @"Hamburg"]]
                  };
    }

+ (NSValueTransformer *)populationInCitiesJSONTransformer {
    return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CountryInfo.class];
}

当我运行我的应用程序时,我收到了一个错误:

代码语言:javascript
复制
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'populationInCities must either map to a JSON key path or NSNull, got: {
    populationInCities =     (
        Hamburg,
        Berlin,
        Munich
    );
}.'
EN

回答 2

Stack Overflow用户

发布于 2014-12-26 19:30:49

如果要在populationInCities数组中存储总体数字,则需要自定义转换器:

代码语言:javascript
复制
+ (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];
    }];
}
票数 0
EN

Stack Overflow用户

发布于 2015-01-11 21:38:15

很尴尬,但它很简单地用了。-符号。

例如popukationInCities.hamburg

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27045220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档