【问题标题】:Not able to Parse Push notification json in iOS?无法在 iOS 中解析推送通知 json?
【发布时间】:2016-04-11 12:20:17
【问题描述】:

我从我的服务器推送通知中得到这个,这是 NSDictionary 格式的。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {

     NSLog(@"Recieved remote notification  %@", userInfo);
}

{
    aps =     {
        alert = "{\n      \"GCM\":{\n        \"data\":{\n          \"notificationType\":\"order\",\n          \"oid\":\"CRN14333313\",\n          \"c\":\"allocated\",\n          \"f\":\"253.633333333333\"   }\n      }\n    }";
    };
}

我正在尝试将其转换为没有任何“\”或“\n”的普通 NSDictionary。我该怎么做?

这种 NSDictionary 格式的预期结果

{
    aps =     {
        alert = "{ 
                    "GCM"={ 
                         "data"={ 
                             "notificationType"="ons",
                             "oid"="N14333313",
                             "c"="allocated",
                             "f"="253.633333333333",

                            } 
                        }
                   }";
         };
}

【问题讨论】:

  • 你打印这个NSLog(@"Recieved remote notification %@", userInfo);的回复一次
  • 是的,我正在这样做
  • 请原谅......
  • 那么您在alert 字段中收到了一个JSON?那看起来不太好。查看您发送的 APNS JSON 有效负载。

标签: ios objective-c json nsdictionary


【解决方案1】:

试试这个,

NSError *jsonError;
NSData *objectData = [@"{\n      \"GCM\":{\n        \"data\":{\n          \"notificationType\":\"order\",\n          \"oid\":\"CRN14333313\",\n          \"c\":\"allocated\",\n          \"f\":\"253.633333333333\"   }\n      }\n    }" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                     options:NSJSONReadingMutableContainers
                                                       error:&jsonError];
NSLog(@"dictionary is : %@",json);

我已将您的字符串保持原样。您正在获取 json 字符串作为响应,因此您需要像这样将其转换为 json 对象。希望这能解决您的问题。 :)

【讨论】:

    【解决方案2】:

    请检查服务器数据的格式,不是json格式,只是简单的字符串

    如果你想解析这些数据,那么试试这个,我认为它对你有帮助

    NSString *alert_string[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
    
    NSData *jsondata=[alert_string dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dictionary=[NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments error:nil];
    

    通过使用它,您可以将警报转换为所需格式

    【讨论】:

      【解决方案3】:

      试试这个

      NSDictionary *dictionary = userInfo[@"aps"];

      【讨论】:

      • 我的问题是转义字符
      • NSDictionary *dictionary = userInfo[@"aps"][@"alert"][@"GCM"]; NSLog(@"dict: %@", 字典); -[__NSCFString objectForKeyedSubscript:]: 无法识别的选择器发送到实例 0x1503ae6c0
      【解决方案4】:

      您的有效载荷不正确!您不应该在警报中发送 json。那里的文本将在设备上显示为“通知”。

      这是带有数据的正确有效负载的外观:

      {
          "aps" : {
              "alert" : {
                  "title" : "Game Request",
                  "body" : "Bob wants to play poker",
                  "action-loc-key" : "PLAY"
              },
              "badge" : 5
          },
          "acme1" : "bar",
          "acme2" : [ "bang",  "whiz" ]
      }
      

      如果你的有效载荷是正确的,那么你的字典也是正确的。你不需要解析任何东西。

      即,在您的情况下,您从服务器发送的有效负载应如下所示(注意,这不是 JSON,而是对您的 JSON 外观的伪描述):

      {
          aps =     {
              alert = "Text that will show up as a push notification";
               };
          GCM={ 
                  "data"={ 
                        "notificationType"="ons",
                        "oid"="N14333313",
                        "c"="allocated",
                        "f"="253.633333333333",
                         } 
               }
      }
      

      然后你就可以得到你的 GCM 字典了

      NSDictionary *dictionary = userInfo[@"GCM"];
      

      有关有效负载的更多信息,请参阅此链接:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

      【讨论】:

        猜你喜欢
        • 2013-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多