0

所以我刚刚开始使用 youtube 数据 API 和 JSONModel。以他们YouTubeBrowserDemo为出发点:https ://github.com/JSONModel/YouTubeBrowserDemo 。

所以它工作正常,除了有时某些视频的 MediaThumnail 条目没有time条目。有些返回的视频有,有些没有。我对此很好,当然可以在显示数据时编写简单的检查,但是当arrayOfModelsFromDictionaries被调用以将返回的 JSON 转换为我的模型时,我收到以下错误,并且如果只有一个 JSON,则没有任何 JSON 被转换(或更多)time条目丢失:

[JSONModel.m:205] Incoming data was invalid [MediaThumbnail initWithDictionary:]. Keys missing: {(
    time
)}

如何使这种转换不是全有或全无的转换?映射时的某种条件检查?或者是否有其他JSONModel方法已经做到了这一点?


这是 API 调用并尝试转换生成的 JSON:

 [JSONHTTPClient getJSONFromURLWithString:searchURL
                                  completion:^(NSDictionary *json, JSONModelError *err) {


                                      //got JSON back
                                      NSLog(@"Got JSON from web: %@", json);

                                      if (err) {

                                          NSLog(@"err = %@",err);



                                          [[[UIAlertView alloc] initWithTitle:@"Error"
                                                                      message:[err localizedDescription]
                                                                     delegate:nil
                                                            cancelButtonTitle:@"Close"
                                                            otherButtonTitles: nil] show];
                                          return;
                                      }




                                      //initialize the models 
                                      // THIS IS WHERE IT SOMETIMES RETURNS OBJECTS INTO SELF.OBJECTS IF ALL THE MEDIATHUMBNAILS AHVE TIME ENTRIES, OR NOTHING IF ONE OR MORE ARE MISSING
                                      self.objects = [VideoModel arrayOfModelsFromDictionaries:
                                                      json[@"feed"][@"entry"]
                                                      ];




                                      if (self.objects) {
                                          NSLog(@"Loaded successfully models");

                                      }

这是我的 VideoModel.h 与他们的接近 - 仅添加了一个属性:

@interface VideoModel : JSONModel

@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) NSArray<VideoLink>* link;
@property (strong, nonatomic) NSArray<MediaThumbnail>* thumbnail;
@property (strong, nonatomic) NSArray<Author>* author;

@end

视频模型.m:

@implementation VideoModel

+(JSONKeyMapper*)keyMapper
{
    return [[JSONKeyMapper alloc] initWithDictionary:@{
            @"media$group.media$thumbnail":@"thumbnail",
            @"title.$t": @"title"
            }];
}

@end

还有我的 MediaThumnail.h:

@interface MediaThumbnail : JSONModel

@property (strong, nonatomic) NSURL* url;
@property (assign, nonatomic) int width;
@property (assign, nonatomic) int height;
@property (strong, nonatomic) NSString* time;

@end

媒体缩略图.m:

@implementation MediaThumbnail

@end
4

2 回答 2

3

使用 JSONModel 可以很容易地使您的模型的一些属性成为可选 - 在这里查看示例(有关同一页面上不同功能的更多示例):

https://github.com/icanzilb/JSONModel#optional-properties-ie-can-be-missing-or-null

总而言之,您需要做的就是将 <Optional> 协议添加到您的属性中,如下所示:

@property (strong, nonatomic) NSString<Optional>* time;

而已。不要忘记在使用属性时首先检查它是 nil 还是有值。

于 2014-02-04T09:53:37.607 回答
0

创建一个 NSNull+JSON 类别并将其导入 JSONModel.m 或 {ProjectName}-Prefix.pch

#import <Foundation/Foundation.h>

@interface NSNull (JSON)

@end

#import "NSNull+JSON.h"

@implementation NSNull (JSON)

- (NSUInteger)length { return 0; }

- (NSInteger)integerValue { return 0; };

- (NSInteger)intValue { return 0; };

- (float)floatValue { return 0; };

- (NSString *)description { return @"0(NSNull)"; }

- (NSArray *)componentsSeparatedByString:(NSString *)separator { return @[]; }

- (id)objectForKey:(id)key { return nil; }

- (BOOL)boolValue { return NO; }

@end

我一直在我的许多项目中使用它。我更喜欢使用这个过程,所以即使我的服务器为 API 响应引入了任何新的 JSON 密钥,JSON 解析仍然可以正常工作,而不会出现任何崩溃或问题......

于 2019-02-04T08:48:45.397 回答