2013-12-16 177 views
-1

我正在從http發佈http到我的web服務,這是寫在.NET平臺上。Objective-C Json反序列化(空值不能被反序列化)

我使用JSONModel來序列化/反序列化和AFNetworking發出http請求。

我可以提出請求並獲得響應。但是當響應數據上的任何變量爲null時就會發生問題。

這裏是我打電話給我的web服務:

NSDictionary *parameters = @{@"Username":username,@"Password":password}; 
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
[manager POST:@"<my_url>" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    // Request succeeded. 
    LoginResponse *response =[[LoginResponse alloc]initWithDictionary:responseObject error:nil]; 
    // !!! "response" is null here because of Message on response data is null. 
}failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    // Request failed. 
}]; 

這裏是我的LoginResponse.h文件:

@interface LoginResponse : JSONModel 

@property (nonatomic, strong) NSString *IsSuccess; 
@property (nonatomic, strong) NSString *Message; 

@end 

這是Web服務的回報,當我通過小提琴手要求:

{"IsSuccess":true,"Message":null} 

因爲我對響應數據有空值,所以反序列化不起作用。當我刪除(因爲消息響應數據爲空)@property (nonatomic, strong) NSString *Message;LoginResponse.h文件,反序列化工作正常。

我不在乎應答數據是否有空變量,我只是想反序列化它。我無法估計出什麼問題。爲什麼在響應有空變量時反序列化不起作用?

任何幫助,將不勝感激。

回答

0

聲明Message可選在LoginResponse

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

更多信息,請參見JSONModel docs

+0

哎呀,我錯過了它!謝謝。 – JustWork