JSONModel returns nil

前端 未结 1 1468
离开以前
离开以前 2021-01-22 12:56

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:

1条回答
  •  臣服心动
    2021-01-22 13:23

    Explanation:

    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.

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

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

    What you have in fact at: http://api.matematikfessor.dk/apps/teacher_videos is

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

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

    Solution:

    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) introduce a new key in your JSON feed - i.e. alter the JSON to be in the form of {"tutorials": [obj1, obj2, etc...]}

    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);
      }];
    

    0 讨论(0)
提交回复
热议问题