问题
I have a model using JSONModel in my objective c application. JSONModel github I am trying init my model from a response of server. The response of server is this:
[ { "id": 0, "name": "Jhon" }, { "id": 1, "name": "Mike" }, { "id": 2, "name": "Lua" } ]
My JSONModel is:
@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
And I'm trying init this then get the response from server like:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseData options:NSJSONWritingPrettyPrinted error:&error];
People *peoplemodel = [[People alloc] initWithData:jsonData error:&error];
But I'm getting a null model.
I think that the problem is the format response like
[{ }]
But I don't know how to convert this.
is possible init a JSONModel from an array of json objects?
How can I do this?
回答1:
The library you reference appears to only be compatible with an NSDictionary root Json object, whereas you have an NSArray root json object.
https://github.com/jsonmodel/jsonmodel/blob/master/JSONModel/JSONModel/JSONModel.m#L123
https://github.com/jsonmodel/jsonmodel/blob/master/JSONModel/JSONModel/JSONModel.m#L161
If you check the error returned when attempting to initWithData, I'm sure it will have this error message:
Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'.
Your currently server JSON response:
NSArray <NSDictionary *> *jsonArray = @[ @{ @"id": @(0), @"name": @"Jhon" }, @{ @"id": @(1), @"name": @"Mike" }, @{ @"id": @(2), @"name": @"Lua" } ];
An example of what the JSON would look like that the JSONModel lib would be able to parse:
NSDictionary <NSString *, NSArray <NSDictionary *> *> *jsonDictionary = @{ @"peopleArray": @[@{@"id": @(0), @"name": @"Jhon"}, @{ @"id": @(1), @"name": @"Mike" }, @{ @"id": @(2), @"name": @"Lua" }]};
If you're unable to modify your server response on the backend to have it return an NSDictionary as the root JSON object, you could pre-process the returned data to format for what JSONModel lib is expecting (NSDictionary root).
Here's an example of what I mean (specifically you'll want to use something like jsonDataUsingYourCurrentArrayStructureWithPreProcessing:
to pre-process your JSON data:
- (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;
}
}
Alternatively, you can just roll your own initialization from the JSON array, something along these lines in the People Class:
+ (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];
}
来源:https://stackoverflow.com/questions/56212016/how-to-init-a-jsonmodel-with-an-array-of-json-objects