【问题标题】:iOS - Convert IFNULL(...) JSON key into object attributeiOS - 将 IFNULL(...) JSON 键转换为对象属性
【发布时间】:2015-12-22 08:31:40
【问题描述】:

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

"word_count" = 947;

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

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

这使我的应用程序崩溃,因为我的 JSONModel 库希望每篇文章都有一个 word_count 属性,因此它可以使其成为具有 wordCount 属性的 Article 对象。我该如何解决这个问题?

以下是我的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 对象以返回一个列表:

/*!
 * @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";
...
...
...

【问题讨论】:

  • 您能重新表述一下问题所在吗?您能否包含导致模型崩溃的 JSON?
  • 没问题,一秒钟。
  • @MarinTodorov 我已经用我正在使用的代码和我得到的 JSON 响应示例更新了我的帖子。
  • JSON 响应的字数统计部分过去只是“word_count” = 某物,但由于某种原因现在它是“IFNULL(e.word_count, 0)”。这使我的应用程序崩溃,因为 JSONModel 不断给我“缺少键:wordCount”错误,因为它无法将其转换为属性对象,因为 Pocket 现在以不同的方式返回它。

标签: ios json jsonmodel pocket


【解决方案1】:

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

【讨论】:

  • 我认为你是对的。我又试了一次,它开始给我正确的“word_count”属性,而不是让我的应用程序崩溃的那个。我会将此报告给 Pocket API 团队,以防万一这可能是一个随机问题。但现在,我只需将wordCount 属性标记为Optional,以防将来再次随机发生这种情况。谢谢!
猜你喜欢
  • 2020-08-03
  • 2022-11-01
  • 2020-02-02
  • 2018-10-14
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
相关资源
最近更新 更多