我正在使用Pocket
API 并获取用户的文章。我使用JSONModel
我找到的库来使用从 JSON 响应中获得的属性GitHub
来制作对象。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
对象以返回一个列表:
/*!
* @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";
...
...
...