gpt4 book ai didi

ios - 如何在 objective-c 中将 NSString 转换为 NSDictionary?

转载 作者:行者123 更新时间:2023-11-28 18:59:04 30 4
gpt4 key购买 nike

您好,我是 Objective-C 的新手,在此先感谢您的帮助。

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{

NSLog(@"Notification Received: %@", userInfo);
NSString *NSSMessage = [NSString stringWithFormat:@"%@", userInfo];

NotificationMessageHandler *obj = [[NotificationMessageHandler alloc] init];
[obj handleNotificationMessage: NSSMessage]; // NSSmessage is a string to convert into JSON

}

我在 NSSMessage 中跟随 NSString

{
aps = {
alert = "\"Status\":\"Confirm\",\"Location\":\"\",\"Type\":\"Telephone Consultation\",\"Date\":\"2015-01-05\",\"Time\":\"19:03\",\"RescheduleDate\":\"\"";
badge = 1;
sound = "beep.wav";
};
}

在 handleNotificationMessage 内部,正在将 NSString 转换为 JSON 格式

- (void) handleNotificationMessage : (NSString *) NSSParam
{

if(NSSParam != nil && NSSParam != Nil)
{

// Parse the string
NSLog(@"NSS Message Param %@", NSSParam);


NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@" "
withString:@""];
NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@"\n"
withString:@""];


NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@"aps="
withString:@"\"aps\":"];
NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@"alert=\""
withString:@"\"alert\":{"];
NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@"\"\";"
withString:@"\"},"];

NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@"badge="
withString:@"\"badge\":"];
NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@"sound="
withString:@"\"sound\":"];

NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@"}}\""
withString:@"}}"];

NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@";"
withString:@","];
NSSParam = [NSSParam stringByReplacingOccurrencesOfString:@",},}"
withString:@"}}"];

NSLog(@"NSS Message Param After Parsing %@", NSSParam);

NSData *data = [NSSParam dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; // Here json is null
}
}

解析后我有一个 JSON 如下

{"aps":{"alert":{\"Status\":\"Confirm\",\"Location\":\"\",\"Type\":\"TelephoneConsultation\",\"Date\":\"2015-01-05\",\"Time\":\"20:15\",\"RescheduleDate\":\"\"},"badge":1,"sound":"beep.wav"}}

我使用以下代码将 NSString 转换为 JSON

NSData *data = [NSSParam dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; // Here json is null

这里 json 对象显示为 null 但 NSData 有对象。那么,如何在 objective-c 中将 NSString 转换为 NSDictionary?请帮忙!

最佳答案

使用下面的代码

NSError *error;
NSString *dictString=[NSString stringWithFormat:@"%@", userInfo];//or ur dict reference..
NSData *jsonData = [dictString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&error];

//(OR)..如果你想将警报值形成一个字典。使用如下

NSString *jsonString = [json objectForKey:@"alert"];
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

希望对您有所帮助...!

关于ios - 如何在 objective-c 中将 NSString 转换为 NSDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28345531/

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