使用 NSJSONSerialization 将 NSString 转换为 JSON 无效

Converting NSString to JSON with NSJSONSerialization is not working

我有这个功能:

- (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 并询问它是否有效,它总是 returns false。

我已尝试替换字符串中的“\”,但没有成功。

[tPayload stringByReplacingOccurrencesOfString:@"\\"" withString:@""]

我尝试用字符串创建 NSMutableDictionary,但结果不是字典。

NSMutableDictionary *tPayload = [tJSONResponse objectForKey:@"Payload"];

我有点迷路了。

任何帮助将不胜感激。

此致。

方法 NSJSONSerialization.isValidJSONObject: 检查对象(例如 NSDictonaryNSArray 实例)是否可以转换 JSON.它不检查 NSData 实例是否可以从 JSON 转换为 。对于 NSData,它总是 return false。

所以只需调用 NSJSONSerialization.JSONObjectWithData:options: 并检查结果即可。

问题出在这一行

[NSJSONSerialization isValidJSONObject:tPayloadData]

来自 isValidJSONObject

的文档

Returns a Boolean value that indicates whether a given object can be converted to JSON data.

给定对象表示NSArrayNSDictionary不是NSData

删除检查并实施 JSONObjectWithDataoptions:error:

中的错误参数