iOS - 将 IFNULL(...) JSON 键转换为对象属性

iOS - Convert IFNULL(...) JSON key into object attribute

我正在与 Pocket API 合作并获取用户的文章。我使用在 GitHub 上找到的 JSONModel 库,使用从 JSON 响应中获得的属性来制作 Article 对象。每个 Article 对象都有一个 wordCount 属性,该属性过去很容易从 Pocket 的 API 响应的一部分分配:

"word_count" = 947;

但是现在,出于某种原因,Pocket 改变了它给出字数的方式,现在它给出了以下内容:

"IFNULL(e.word_count, 0)" = 947;

这让我的应用程序崩溃了,因为我的 JSONModel 库希望每篇文章都有一个 word_count 属性,这样它就可以将它变成一个 Article 对象,并带有 wordCount属性。我该如何解决这个问题?

以下是我的 Article.h 对象设置代码:

#import "JSONModel.h"

@class Article;

@interface Article : JSONModel

@property (strong, nonatomic) NSString<Optional> *newsSource;
@property (nonatomic, strong) NSString *givenUrl;
@property (strong, nonatomic) NSString *resolvedUrl;
@property (nonatomic, strong) NSString *resolvedTitle;
@property (nonatomic, strong) NSString *excerpt;
@property (strong, nonatomic) NSDictionary<Optional> *image;
@property (nonatomic, strong) NSNumber *wordCount;
@property (strong, nonatomic) NSString<Optional> *fullText;
@property (strong, nonatomic) NSString<Optional> *summary;
@property (strong, nonatomic) NSString *itemId;
@property (strong, nonatomic) NSNumber *favorite;
@property (strong, nonatomic) NSNumber *hasImage;
@property (strong, nonatomic) NSNumber *status;
@property (strong, nonatomic) NSDictionary<Optional> *authors;
@property (strong, nonatomic) NSNumber *sortId;

+(void)retrieveArticlesWithState:(NSString*)state andFavorite:(NSString *)favorite andOffset:(NSNumber*)offsetInteger successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;

上面的代码让 JSONModel 知道从 JSON 响应中期望得到什么属性,并将其转化为每个 Article 对象的属性。

在我的 Article.m 文件中,我使用以下方法实现该方法:

+(void)retrieveArticlesWithState:(NSString*)state andFavorite:(NSString *)favorite andOffset:(NSNumber*)offsetInteger successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error
{
    [self letPocketRetrieveArticlesWithState:state andFavorite:favorite andOffset:offsetInteger successCallback:[Article modelListCallback:success] errorCallback:error];
}

此方法帮助我将 word_count 等属性转换为 wordCount:

#pragma mark - helpers

//Built in option for JSONModel to turn things like sort_id into sortId
+(JSONKeyMapper*)keyMapper
{
    return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
}

此代码帮助我始终如一地将多个 Article 对象转换为 return 列表:

/*!
 * @brief Helper method to get the same block to consistently turn API response into article models.
 * @param callback A block to call once the article has been parsed from the API response.
 * @return A block to be passed along to API requests for parsing out API responses consistently.
 */
+(void(^)(NSDictionary*))modelListCallback:(void (^)(NSArray*))callback {
    return ^(NSDictionary* json){
        NSError* err = nil;
        NSMutableArray* articles = [[NSMutableArray alloc] init];
        for(NSDictionary* article in [[json objectForKey:@"list"] allValues]) {
            [articles addObject:[[Article alloc] initWithDictionary:article error:&err]];
        }
        callback(articles);
    };
}

这是我得到的 JSON 响应示例的一部分:

{
    complete = 1;
    error = "<null>";
    list =     {
        1027166096 =         {
            "IFNULL(e.word_count, 0)" = 2930;
            authors =             {
                37936109 =                 {
                    "author_id" = 37936109;
                    "item_id" = 1027166096;
                    name = "Telegraph Reporters";
                    url = "http://www.telegraph.co.uk/authors/telegraph-reporters/";
                };
            };
            excerpt = "In his third year at Oxford, as an undergraduate studying biology and physiology, Oliver Sacks was not yet 20 years old when he first decided to take LSD.";
            favorite = 0;
            "given_title" = "Oliver Sacks' most mind-bending experiment";
...
...
...

这看起来像是他们这边的错误..."IFNULL(e.word_count, 0)" 是一个 SQL 代码,不应作为键出现在 JSON 响应中。如果你有办法,你应该向他们报告这个错误