无法用地幔映射无钥匙的json(正确)

时间:2015-03-02 11:48:51

标签: ios json github-mantle

我正在尝试找到一种方法来正确解析我的json数组。我无法找到一个很好的解决方案来使用地幔解析我的json对象。我能够找到有关父子对象的信息,但不能找到有关无密钥对象集合的信息。我的代码有效,但由于视图控制器文件中存在太多与数据相关的代码,因此它不是理想的方式。如果有人可以建议一个更好的方法来表达这一点(我的意见更加有条理,以至于我不必使用for循环来覆盖视图控制器中的每个项目),我将不胜感激。

这就是我的json看起来的样子(没有数组中对象的键):

 [
    - {
    id: 40,
    owner: 
    {
    login: “alex",
    id: 9,
    name: “alex",
    avatar: “url here", 
    url: "url ",
    games_url: "url",
    type: "User",
    site_admin: false
    },
    name: “test",
    compliant: false,
    published: false,
    url: “url",
    tags_url:"url",
    subscribers_url: "url",
    cover_url:”url",
    type: “Game"
    },

    {
    id: 40,
    owner: 
    - {
    login: “alex",
    id: 9,
    name: “alex",
    avatar: “url here",
    url: "url ",
    games_url: "url",
    type: "User",
    site_admin: false
    },
    name: “test",
    compliant: false,
    published: false,
    url: “url",
    tags_url:"url",
    subscribers_url: "url",
    cover_url:”url",
    type: “Game"
    },

    {
      more items like this...
    }
 ]

这是代码

restmanager.m

 - (void)getUserInfo:(void (^)(NSArray *))successBlock failure:(void (^)(NSError *))failureBlock
{
  // gets the data from server via afnetworking and sends it via successblock
}

模型类

abcmodel.h

#import "MTLModel.h"
#import <MTLJSONAdapter.h>

@interface abcmodel : MTLModel <MTLJSONSerializing>

@property (nonatomic, copy, readonly) NSString   *name;
@property (nonatomic, copy, readonly) NSURL   *coverUrl;

@end

abcmodel.m

#import "abcmodel.h"
#import <NSValueTransformer+MTLPredefinedTransformerAdditions.h>
#import <MTLValueTransformer.h>

@implementation abcmodel

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return  @{
              @"name":@"name",
              @"coverUrl":@"coverUrl"
              };
}

@end

viewcontroller.m

@property ( nonatomic, strong ) NSMutableArray *abcs;


- (void)viewDidLoad
{
    [super viewDidLoad];

   [restmanager sharedInstance]fetchData:.....]{
           abcmodel *abcItem;
            for (NSDictionary *abcmodelJSON in responseObject)
            {
                NSError *error;

                abcItem = [MTLJSONAdapter modelOfClass:[abcmodel class]fromJSONDictionary:abcmodelJSON error:&error];

                if (abcItem)
                {
                    [self.abcs addObject:abcItem];

                }else
                {
               //log error
                }

            }

     }

}

//然后将其显示在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

谢谢。

0 个答案:

没有答案