如何用json对象启动jsonmodel



我在目标C应用中使用JSONMODEL具有模型。jsonmodel github我正在尝试从服务器的响应中启动我的模型。服务器的响应是:

[ { " id":0, "名称":" Jhon" },, { " id":1, "名字":"迈克" },, { " id":2, "名称":" lua" }]

我的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

我正在尝试init,然后从服务器中获取响应:

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

,但我正在获得无效的模型。

我认为问题是格式响应,例如

[{}]

,但我不知道如何转换。

可能会从json对象数组中启动jsonmodel?

我该怎么做?

您引用的库似乎与nsdictionary root json对象兼容,而您具有nsarray root json对象。

https://github.com/jsonmodel/jsonmodel/blob/master/jsonmodel/jsonmodel/jsonmodel/jsonmodel.m#l123

https://github.com/jsonmodel/jsonmodel/blob/master/jsonmodel/jsonmodel/jsonmodel/jsonmodel.m#l161

>

如果您在尝试到initwithdata时返回的错误,我敢肯定它将具有此错误消息:

无效的JSON数据:尝试使用initwithDictionary初始化JSONMODEL对象:错误:但是字典参数不是'nsdictionary'。

您当前的服务器JSON响应:

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

JSON的示例是JSONMODEL LIB可以解析的示例:

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

如果您无法在后端修改服务器响应以使其返回nsdictionary作为root json对象,则可以将返回的数据预先处理为jsonmodel lib所期望的(nsdictionary root(的格式。<<<<<<<<<<<<<<<<

这是我的意思的一个示例(特别是您要使用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数组中滚动自己的初始化,这是人们班级中的这些行:

+ (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];
}

也许为时已晚,但仅适用于将来的读者。JSONMODEL具有特定方法:

  • fromdictionaries andRayofModels
  • ArrayofModelsFromData
  • ArrayofModels fromstring

https://github.com/jsonmodel/jsonmodel/blob/master/jsonmodel/jsonmodel/jsonmodel/jsonmodel.m#l1055

最新更新