我有这个功能:
- (void)checkLogin:(NSString *)pLogin andPassword:(NSString*) pPassword {
//Create the data object.
NSMutableDictionary *tLoginAndPasword = [NSMutableDictionary dictionaryWithObjectsAndKeys:pLogin,@"Login",pPassword,@"Password", nil];
NSMutableDictionary *tData = [NSMutableDictionary dictionaryWithObjectsAndKeys:[_Util serializeDictionary:tLoginAndPasword],@"Data", nil];
//Call the login method.
NSData *tResponse = [_Util getLogin:tData];
if (tResponse != Nil) {
_oLabelErrorLogin.hidden = YES;
[_Util setUser:pLogin andPassword:pPassword];
NSMutableDictionary *tJSONResponse =[NSJSONSerialization JSONObjectWithData:tResponse options:kNilOptions error:nil];
NSString *tPayload = [tJSONResponse objectForKey:@"Payload"];
if([[tJSONResponse objectForKey:@"StatusCode"] isEqual: @"424"]) {
//Set global values.
NSData *tPayloadData = [tPayload dataUsingEncoding:NSUTF8StringEncoding];
if ([NSJSONSerialization isValidJSONObject:tPayloadData]) {
_Payload = [NSJSONSerialization JSONObjectWithData:tPayloadData options:kNilOptions error:nil];
_RowCount = _Payload.count;
} else {
NSLog(@"JSON Wrong String %@",tPayload);
}
} else if([[tJSONResponse objectForKey:@"StatusCode"] isEqual: @"200"]){
_Payload = Nil;
}
} else {
//Set global values.
_Payload = Nil;
_oLabelErrorLogin.hidden = NO;
//Clear login data.
_oLogin.text = @"";
_oPassword.text = @"";
[_Util setUser:@"" andPassword:@""];
}
}
JSON响应如下所示:
{
"Payload": "{\"UserName\":\"Marco Uzcátegui\",\"Clients\":[{\"UserProfileId\":4,\"ProfileName\":\"Platform Administrator\",\"ClientName\":\"Smart Hotel Platform\",\"InSession\":true},{\"UserProfileId\":5,\"ProfileName\":\"Administrator\",\"ClientName\":\"La Moncloa de San Lázaro\",\"InSession\":false},{\"UserProfileId\":6,\"ProfileName\":\"Administrator\",\"ClientName\":\"Jardín Tecina\",\"InSession\":false}]}",
"StatusCode": "424",
"StatusDescription": null
}
正如你所看到的,我内部有一个转义字符串" Payload"这是一个正确的JSON,所以我想用该字符串生成另一个NSMutableDictionary,所以我这样做:
NSData *tPayloadData = [tPayload dataUsingEncoding:NSUTF8StringEncoding];
if ([NSJSONSerialization isValidJSONObject:tPayloadData]) {
_Payload = [NSJSONSerialization JSONObjectWithData:tPayloadData options:kNilOptions error:nil];
_RowCount = _Payload.count;
} else {
NSLog(@"JSON Wrong String %@",tPayload);
}
所以我从NSString创建一个NSData并询问它是否有效,它总是返回false。
我试图更换" \"来自字符串并且无法正常工作。
[tPayload stringByReplacingOccurrencesOfString:@"\\\"" withString:@""]
我试图用字符串创建一个NSMutableDictionary,但结果不是字典。
NSMutableDictionary *tPayload = [tJSONResponse objectForKey:@"Payload"];
我有点迷失在这里。
任何帮助将不胜感激。
问候。
答案 0 :(得分:1)
方法NSJSONSerialization.isValidJSONObject:
检查对象(例如NSDictonary
或NSArray
实例)是否可以转换为转换为 JSON。它不会检查NSData
实例是否可以从 JSON转换为。对于NSData
,它将始终返回false。
所以只需致电NSJSONSerialization.JSONObjectWithData:options:
并检查结果。
答案 1 :(得分:1)
问题在于这一行
Org CaseProduct FaultGroup ManufacturingMonth ClosedMonth
DPL REF COMPRESSOR 2012-07-01 2012-07-01
DPL REF COMPRESSOR 2012-07-01 2012-08-01
DPL REF COMPRESSOR 2012-07-01 2012-09-01
DPL REF COMPRESSOR 2012-07-01 2012-10-01
DPL REF COMPRESSOR 2012-07-01 2012-11-01
DPL REF COMPRESSOR 2012-07-01 2012-12-01
DPL REF COMPRESSOR 2012-07-01 2013-01-01
DPL REF COMPRESSOR 2012-07-01 2013-02-01
来自[NSJSONSerialization isValidJSONObject:tPayloadData]
返回一个布尔值,指示给定对象是否可以 转换为JSON数据。
给定对象表示isValidJSONObject
或NSArray
,但不 NSDictionary
删除该检查并在NSData