JSONModel返回nil [英] JSONModel returns nil

查看:263
本文介绍了JSONModel返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSONModel从URL获取JSON. 这是一个非常简单的对象,仅包含2个字符串-名称"和"URL".

I'mm using JSONModel to get the JSON from the URL. It's a very simple object, containing only 2 strings - "name" and "url".

首先,我创建了对象模型:

First I made the Object Model:

@protocol
Tutorial
@end
@interface Tutorial : JSONModel
@property (nonatomic, strong)   NSString *name;
@property (nonatomic, strong)   NSString *url;
@end

然后对象供稿:

#import "JSONModel.h"
#import "Tutorial.h"
@interface TutorialFeed : JSONModel
@property (nonatomic, strong)   NSArray <Tutorial> *tutorials;
@end

,然后在MasterViewController.m中:

and then in MasterViewController.m:

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "TutorialFeed.h"
#import "JSONModelLib.h"

@interface MasterViewController () {
  TutorialFeed *feed;
  TutorialFeed *testFeed;
}
@end
@implementation MasterViewController
-(void)viewDidAppear:(BOOL)animated
{
    feed = [[TutorialFeed alloc]

initFromURLWithString:@"http://api.matematikfessor.dk/apps/teacher_videos"
       completion:^(JSONModel *model, JSONModelError *err) {
           NSLog(@"Tutorials %@", feed.tutorials);
       }];
 }
 @end

问题是,我在日志中返回nil :( 我不确定为什么会这样,因为我设法从URL的JSON中获取数据: Kiwa URL

The problem is, I get returned nil in my log :( Im not sure why is this happening, because I managed to fetch data from JSON from this URL: Kiwa URL

完成所有操作,遵循此教程

All that done, following this tutorial

我不确定我在做什么错. 有人有任何线索吗?

Im not sure what am I doing wrong. Does anybody has any clue ?

推荐答案

说明:

首先,JSONModel希望您的JSON顶级对象是一个字典,只有这样,它才能将其键与模型的属性进行匹配.

First of all JSONModel expects your JSON top object to be a dictionary, only that way it can match its keys to a model's properties.

您的名为TutorialFeed的模型希望接收与属性名称"tutorials"匹配的JSON.这意味着您的JSON feed必须采用以下格式:

Your model called TutorialFeed expects to be fed JSON matching the property name "tutorials".This means your JSON feed must be in the form:

{教程":[{obj1},{obj2},{obj3},...]}

{ "tutorials": [{obj1}, {obj2}, {obj3}, ...] }

您实际上拥有的是: http://api.matematikfessor.dk/apps/teacher_videos

[{{obj1},{obj2},{obj3}]

[{obj1}, {obj2}, {obj3}]

这就是为什么您的模型实例实际上为"nil"的原因,因为JSON结构与您的模型期望的不匹配.

That's why your model instance is in fact "nil" because the JSON structure didn't match what your model expects.

解决方案:

如果您的JSON Feed顶部有一个数组(例如您使用的URL上的数组),则有两个选择:

If you have an array at the top of your JSON feed (like the one on the URL you use) you have two options:

1)在JSON供稿中引入新密钥-即,将JSON更改为{"tutorials":[obj1,obj2等...]}

1) introduce a new key in your JSON feed - i.e. alter the JSON to be in the form of {"tutorials": [obj1, obj2, etc...]}

2)您可以使用另一种方法来解析JSON feed.在此处的文档中查找并使用静态方法解析对象列表:

2) You can use another method to parse the JSON feed. Look up the docs here and use the static method that parses a list of objects:

#import "JSONModel+networking.h"
...
[JSONHTTPClient   
  getJSONFromURLWithString:@"http://api.matematikfessor.dk/apps/teacher_videos"
  completion:^(id feed, JSONModelError *err) {
    NSArray* tutorials = [Tutorial arrayOfModelsFromDictionaries: feed];
    NSLog(@"tutorials: %@", tutorials);
  }];

这篇关于JSONModel返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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