gpt4 book ai didi

ios - 使用 NSJSONSerialization 将 NSString 转换为 JSON 不起作用

转载 作者:行者123 更新时间:2023-11-28 21:33:26 26 4
gpt4 key购买 nike

我有这个功能:

- (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"];

我有点迷路了。

我们将不胜感激。

问候。

最佳答案

问题是这一行

[NSJSONSerialization isValidJSONObject:tPayloadData]

来自isValidJSONObject的文档

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

given object 表示 NSArrayNSDictionary不是 NSData

删除检查并实现 JSONObjectWithDataoptions:error: 中的错误参数

关于ios - 使用 NSJSONSerialization 将 NSString 转换为 JSON 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com