0

我在解析下面的 Moengage 通知响应时遇到问题

从:

   -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
  NSLog(@"notification appdelegate  %@",userInfo);
  [self customPushHandler:userInfo];

}

notification app delegate: { "app_extra" = { screenData = { "" = ""; }; screenName = ""; }; aps = { alert = "iOS Test "; badge = 1; "content-available" = 0; sound = default; }; moengage = { "" = ""; cid = ; }; }

- (void) customPushHandler:(NSDictionary *)notification {

 if (notification !=nil && [notification objectForKey:@"app_extra"] != nil) {
    NSDictionary* app_extra_dict = [notification objectForKey:@"app_extra"];

      NSDictionary* app_extra_dict1 = [[notification objectForKey:@"app_extra"]objectForKey:@"aps"];

      NSDictionary* app_extra_dict2 = [[notification objectForKey:@"aps"];

      NSLog(@"Moenage notification %@",notification);
      NSLog(@"Menage   apps  %@",app_extra_dict1);
      NSLog(@"Moenage apps %@",app_extra_dict2);               
      NSLog(@"Moenage %@",app_extra_dict );

   }
  }

日志:

Moengage 通知:与上述响应相同

管理应用程序(空)

Moenage 应用程序(空)

摩纳哥:

{ screenData = { "" = ""; }; screenName = ""; }

现在我的问题是我正在尝试检索“ aps = { alert = "iOS Test ";" ..但这不是 JSON ..任何人都可以建议我解析这个响应或者是他们 从这个响应中检索“iOS 测试”的方式

4

3 回答 3

1

您获取数据的格式没有问题,无需转换为 JSON,您已经在 NSDictionary 中获取数据。您将字典转换为 JSON 并再次转换 JSON 以获得相同字典的答案没有任何意义。您可以使用以下键简单地访问所有值:

- (void) customPushHandler:(NSDictionary *)notification {

if (notification !=nil && [notification objectForKey:@"app_extra"] != nil) {

    NSDictionary* app_extra_dict = [notification objectForKey:@"app_extra"];
    NSDictionary* aps_dict = [notification objectForKey:@"aps"];

    NSLog(@"Moengage notification : %@",notification);
    NSLog(@"Moengage   appsExtra : %@",app_extra_dict);
    NSLog(@"Moengage aps : %@",aps_dict);
}
}

以下是相同的日志:

    Moengage notification :  {
    "app_extra" =     {
        screenData =         {
            key1 = Val1;
        };
        screenName = Screen1;
    };
    aps =     {
        alert = "Hello!!!";
        badge = 1;
        "content-available" = 0;
        sound = default;
    };
    moengage =     {
        cid = 5715f243597b7b0f37a9254a;
        key1 = Val1;
    };
}

 Moengage   appsExtra :  {
    screenData =     {
        key1 = Val1;
    };
    screenName = Screen1;
}

Moengage aps :  {
    alert = "Hello!!!";
    badge = 1;
    "content-available" = 0;
    sound = default;
}
于 2016-04-19T09:19:21.390 回答
0

尝试像这样打印:

- (void) customPushHandler:(NSDictionary *)notification {
    NSLog(@"notification:%@", notification);
    NSLog(@"Moenage:%@", notification[@"app_extra"]);
    NSLog(@"Menage apps:%@", notification[@"aps"]);
}
于 2016-01-29T06:59:36.150 回答
0

通过将上述响应转换为 Jsonstring 而不是 NSDictionary 解决了这个问题:

   - (void) customPushHandler:(NSDictionary *)notification {

 if (notification !=nil && [notification objectForKey:@"app_extra"] != nil) {

    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notification
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];

    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        NSLog(@"Got jsonString: %@", jsonString);

        NSError *jsonError;
        NSData *objectData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&jsonError];


         NSLog(@"json %@",json[@"aps"][@"alert"]);


    }

}

安慰:

2016-01-29 12:28:06.613 json iOS 测试

于 2016-01-29T12:22:39.257 回答