Ios 将嵌套模型类对象作为JSON api参数传递
我有一个模型类,它包含不同的属性,一些属性还包含另一个模型类的对象。我的问题是我想直接将模型类对象作为JSON api参数传递 请给我指路 前 我想将objUserModel作为参数传递给json api。 请建议我如何将objUserModel作为jsonapiparameters传递Ios 将嵌套模型类对象作为JSON api参数传递,ios,objective-c,Ios,Objective C,我有一个模型类,它包含不同的属性,一些属性还包含另一个模型类的对象。我的问题是我想直接将模型类对象作为JSON api参数传递 请给我指路 前 我想将objUserModel作为参数传递给json api。 请建议我如何将objUserModel作为jsonapiparameters传递 感谢您的改进您可以创建一个模型对象类,并将所有模型类更改为该类的子类。然后将dictionaryWithPropertiesOfObject:的实现更改为如下内容: - (NSDictionary *)dict
感谢您的改进您可以创建一个
模型对象
类,并将所有模型类更改为该类的子类。然后将dictionaryWithPropertiesOfObject:
的实现更改为如下内容:
- (NSDictionary *)dictionaryWithPropertiesOfObject:(id)obj {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned count;
objc_property_t *properties = class_copyPropertyList([obj class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
if([obj valueForKey:key]!=nil){
id targetObject = [obj valueForKey:key];
if ([targetObject isKindOfClass:[ModelObject class]]){
[dict setObject:[self dictionaryWithPropertiesOfObject: targetObject] forKey:key];
}
else if ([targetObject isKindOfClass:[NSArray class]]){
[dict setObject:[self arrayWithDictionariesForObjectsInArray: (NSArray *)targetObject] forKey:key];
}
else {
[dict setObject:targetObject forKey:key];
}
}
}
free(properties);
return [NSDictionary dictionaryWithDictionary:dict];
}
- (NSArray *) arrayWithDictionariesForObjectsInArray: (NSArray *) array {
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
for (id object in array){
[mutableArray addObject:[self dictionaryWithPropertiesOfObject:object]];
}
return [NSArray arrayWithArray:mutableArray];
}
-(NSDictionary*)字典,其属性为对象:(id)obj{
NSMutableDictionary*dict=[NSMutableDictionary];
无符号计数;
objc_property_t*properties=class_copyPropertyList([obj class],&count);
for(int i=0;i
因此,
ModelObject
的所有实例都插入到字典中,并带有它们的字典表示形式。为什么不试试呢?Kazunori,但我不想使用第三方工具。。任何其他建议到目前为止您尝试了什么?我最后使用了这个方法,但它仍然在嵌套的模型对象-(NSDictionary*)dictionaryWithPropertiesOfObject:(id)obj{NSMutableDictionary*dict=[NSMutableDictionary];unsigned count;objc_property_t*properties=class_copyPropertyList([obj class],&count);for(int i=0;i- (NSDictionary *)dictionaryWithPropertiesOfObject:(id)obj {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned count;
objc_property_t *properties = class_copyPropertyList([obj class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
if([obj valueForKey:key]!=nil){
id targetObject = [obj valueForKey:key];
if ([targetObject isKindOfClass:[ModelObject class]]){
[dict setObject:[self dictionaryWithPropertiesOfObject: targetObject] forKey:key];
}
else if ([targetObject isKindOfClass:[NSArray class]]){
[dict setObject:[self arrayWithDictionariesForObjectsInArray: (NSArray *)targetObject] forKey:key];
}
else {
[dict setObject:targetObject forKey:key];
}
}
}
free(properties);
return [NSDictionary dictionaryWithDictionary:dict];
}
- (NSArray *) arrayWithDictionariesForObjectsInArray: (NSArray *) array {
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
for (id object in array){
[mutableArray addObject:[self dictionaryWithPropertiesOfObject:object]];
}
return [NSArray arrayWithArray:mutableArray];
}