如何使用JSON对象数组初始化JSONModel?

时间:2019-05-19 21:08:41

标签: arrays objective-c json jsonmodel

我在我的目标c应用程序中有一个使用JSONModel的模型。 JSONModel github我正在尝试从服务器的响应初始化我的模型。服务器的响应是这样的:

  

[       {           “ id”:0,           “名称”:“ Jhon”       },       {           “ id”:1           “名称”:“迈克”       },       {           “ id”:2           “名称”:“卢阿”       }]

我的JSONModel是:

@protocol People @end

@interface People : JSONModel

@property (nonatomic, strong)  NSArray <Person> * peopleArray;

@end





@protocol Person @end

@interface Person : JSONModel

@property (nonatomic, strong)  NSNumber <Optional>  * id;

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

@end

我正在尝试初始化它,然后从服务器获取响应:

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseData options:NSJSONWritingPrettyPrinted error:&error];
People *peoplemodel = [[People alloc] initWithData:jsonData error:&error];

但是我得到一个空模型。

我认为问题在于格式响应

  

[{}]

但是我不知道该如何转换。

是否可以从json对象数组中初始化JSONModel?

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您引用的库似乎仅与NSDictionary根Json对象兼容,而您具有NSArray根json对象。

https://github.com/jsonmodel/jsonmodel/blob/master/JSONModel/JSONModel/JSONModel.m#L123

https://github.com/jsonmodel/jsonmodel/blob/master/JSONModel/JSONModel/JSONModel.m#L161

如果您检查尝试初始化initWithData时返回的错误,我确定它将显示以下错误消息:

  

无效的JSON数据:尝试使用initWithDictionary:error初始化JSONModel对象,但dictionary参数不是'NSDictionary'。

您当前的服务器JSON响应:

NSArray <NSDictionary *> *jsonArray = @[ @{ @"id": @(0), @"name": @"Jhon" }, @{ @"id": @(1), @"name": @"Mike" }, @{ @"id": @(2), @"name": @"Lua" } ];

有关JSONModel库能够解析的JSON的示例:

NSDictionary <NSString *, NSArray <NSDictionary *> *> *jsonDictionary = @{ @"peopleArray": @[@{@"id": @(0), @"name": @"Jhon"}, @{ @"id": @(1), @"name": @"Mike" }, @{ @"id": @(2), @"name": @"Lua" }]};

如果您无法在后端上修改服务器响应以使其返回NSDictionary作为根JSON对象,则可以对返回的数据进行预处理,以格式化为JSONModel lib期望的格式(NSDictionary根)。 / p>

这是我的意思的示例(具体地说,您将希望使用jsonDataUsingYourCurrentArrayStructureWithPreProcessing:之类的东西来预处理JSON数据:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray <NSDictionary *> *jsonArray = @[ @{ @"id": @(0), @"name": @"Jhon" }, @{ @"id": @(1), @"name": @"Mike" }, @{ @"id": @(2), @"name": @"Lua" } ];
    NSError *jsonSerializationError;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray options:0 error:&jsonSerializationError];
    if (jsonSerializationError) {
        NSLog(@"jsonSerializationError = %@", jsonSerializationError);
    }
    jsonData = [self jsonDataUsingYourCurrentArrayStructureWithPreProcessing:jsonData];
    NSError *jsonModelError;
    People *people = [[People alloc] initWithData:jsonData error:&jsonModelError];
    if (people) {
        [self printPeople:people];
    } else if (jsonModelError != nil) {
        NSLog(@"Error returned from jsonModel = %@", jsonModelError);
    }
}

- (void)printPeople:(People *)people {
    for (Person *person in people.peopleArray) {
        NSLog(@"person id = %li, name = %@", [person.id integerValue], person.name);
    }
}

- (NSData *)jsonDataUsingYourCurrentArrayStructureWithPreProcessing:(NSData *)jsonData {
    NSError *parsingError;
    id obj = [NSJSONSerialization JSONObjectWithData:jsonData
                                             options:0
                                               error:&parsingError];
    if ([obj isKindOfClass:[NSArray class]] && parsingError == nil) {
        NSArray <NSDictionary *> *jsonArray = (NSArray <NSDictionary *> *)obj;
        NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject:jsonArray forKey:@"peopleArray"];
        NSError *jsonSerializationError;
        NSData *jsonDictData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&jsonSerializationError];
        if (jsonDictData && jsonSerializationError == nil) {
            return jsonDictData;
        } else {
            NSLog(@"jsonSerializationError = %@", jsonSerializationError);
            return nil;
        }
    } else {
        if (parsingError) {
            NSLog(@"Error parsing jsonData = %@", parsingError);
        }
        return nil;
    }
}

或者,您可以从JSON数组中进行自己的初始化,在People类中遵循以下几行:

+ (instancetype)peopleWithJsonArray:(NSArray<NSDictionary *> *)jsonArray {
    People *people = [[People alloc] init];
    if (people) {
        [people setupPeopleArrayFromJsonArray:jsonArray];
    }
    return people;
}

- (void)setupPeopleArrayFromJsonArray:(NSArray <NSDictionary *> *)jsonArray {
    NSMutableArray <Person *> *people = [[NSMutableArray alloc] initWithCapacity:jsonArray.count];
    for (NSDictionary *personDictionary in jsonArray) {
        Person *person = [Person personWithID:[personDictionary objectForKey:@"id"] andName:[personDictionary objectForKey:@"name"]];
        [people addObject:person];
    }
    self.peopleArray = [NSArray arrayWithArray:people];
}