JSONModel arrayOfModelsFromDictionaries-缺少关键点时出错 [英] JSONModel arrayOfModelsFromDictionaries -- error when key is SOMETIMES missing

查看:467
本文介绍了JSONModel arrayOfModelsFromDictionaries-缺少关键点时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚开始使用youtube数据API和JSONModel.以他们的YouTubeBrowserDemo作为起点: https://github.com/JSONModel/YouTubeBrowserDemo ./p>

因此,它工作正常,但有时某些视频的MediaThumnail条目没有time条目.有些返回的视频有它们,有些则没有.我对此表示满意,当然可以在显示数据时编写简单的检查,但是当调用arrayOfModelsFromDictionaries将返回的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

VideoModel.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

MediaThumnail.m:

@implementation MediaThumbnail

@end

解决方案

使用JSONModel可以很容易地将模型的某些属性设置为可选-在此处查看示例(有关同一页面上不同功能的更多示例):

https://github.com/icanzilb/JSONModel#optional-properties-ie可能会丢失或为空

所有您需要做的就是添加< Optional>您财产的协议,如下所示:

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

就是这样.使用属性首先检查它是否为nil或具有值时,请不要忘记.

So I'm just getting started with the youtube data API and JSONModel. Using their YouTubeBrowserDemo as my starting point: https://github.com/JSONModel/YouTubeBrowserDemo.

So it's working fine except sometimes the MediaThumnail entry for certain videos doesn't have a time entry. Some returned videos have them, some don't. I'm fine with this and can of course write simple checks when I'm displaying the data, but when arrayOfModelsFromDictionaries is called to convert the returned JSON to my models, I get the following error and none of the JSON is converted if only one (or more) of the time entries are missing:

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

How do I make this conversion NOT an all-or-nothing conversion? Some kind of conditional check when mapping? Or is there a different JSONModel method that already does this?


Here's the API call and attempt to convert the resulting 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");

                                      }

Here's my VideoModel.h is close to theirs - only one added property:

@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

VideoModel.m:

@implementation VideoModel

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

@end

And my 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

MediaThumnail.m:

@implementation MediaThumbnail

@end

解决方案

It's very easy to make some of your model's properties optional with JSONModel - have a look for an example here (more examples about different features on the same page):

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

All in all what you need to do is add the <Optional> protocol to your property, like so:

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

That's it. Don't forget when using the property to check first if it's nil or has a value.

这篇关于JSONModel arrayOfModelsFromDictionaries-缺少关键点时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆