iOS处理接口返回null,属性名为id等情况

后台返回null

1、AFNetWorking处理

 AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
 manager.responseSerializer = response;//申明返回的结果是json类型
 response.removesKeysWithNullValues = YES;//此方法用来删除value值为null的键值对

2、MJExtension处理

在自己的模型中实现代理MJKeyValue中的方法- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property;

- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property {
        if (oldValue == NULL) {
            if ([property.type.typeClass isKindOfClass:[NSString class]]) {
                  return @"";
              } else if ([property.type.typeClass isKindOfClass:[NSArray class]]) {
                  return @[];
              } else if ([property.type.typeClass isKindOfClass:[NSDictionary class]]) {
                  return @{};
              }
         }
        return oldValue;
}

后台返回中有iOS关键字id

在自己的模型中实现代理MJKeyValue中的方法+ (NSDictionary *)mj_replacedKeyFromPropertyName

+ (NSDictionary*)mj_replacedKeyFromPropertyName {
    return @{@"uid":@"id"};
}

猜你喜欢

转载自blog.csdn.net/liuxinzhongliubin/article/details/137079896