如何在目标c中将NSString转换为NSDictionary?

时间:2015-02-05 13:44:34

标签: ios objective-c json

您是目标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有对象。 那么,如何在目标c中将NSString转换为NSDictionary? 请帮忙!

4 个答案:

答案 0 :(得分:2)

你在那做什么?看看这段代码:

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

userInfo有哪些类型?它是 NSDictionary 。所以你有一个NSDictionary,然后你继续疯狂的追逐,将NSDictionary转换为NSString,以奇怪和奇妙的方式修补该字符串,尝试将其转换为JSON,并尝试解析JSON以获得的NSDictionary 即可。

你能看出那是多么无意义吗?您 NSDictionary 开头。所有代码都毫无意义。

答案 1 :(得分:1)

使用以下代码

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

希望它可以帮助你......!

答案 2 :(得分:1)

首先,您发送的内容似乎是NSDictionary的{​​{1}}方法的结果,例如当你description字典时你会得到什么。这种格式旨在作为传输字典值的方式,而是作为人们阅读的格式。如果可以,您应该安排以更合适的格式发送字典,请在文档中查找序列化。

如果您无法安排更合适的格式,那么您的方法将起作用,但它相当不灵活,因为它与特定消息的内容紧密相关。所有你错过的就是修复转义的双引号。如果你添加:

NSLog

到你的管道的末尾然后它将工作。

HTH

答案 3 :(得分:0)

试试这个:

NSString *jsonString = @"{"aps":{"alert":{\"Status\":\"Confirm\",\"Location\":\"\",\"Type\":\"TelephoneConsultation\",\"Date\":\"2015-01-05\",\"Time\":\"19:03\",\"RescheduleDate\":\"\"},"badge" :1,"sound" :"beep.wav"}}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];