我在解析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 );
}
}日志:
above通知:与上述答复相同 菜单应用程序(null) Moenage应用程序(空) Moenage:
{ screenData = { "" = ""; }; screenName = ""; }
现在我的问题是我正在尝试检索“aps ={iOS=”JSON..can ";“..But --不是JSON..can--请建议我解析这个响应,或者是他们从这个响应中检索"iOS测试”的方法。
发布于 2016-01-29 12:22:39
通过将上面的响应转换为Jsonstring和than 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-04-19 09:19:21
获取数据的格式没有什么问题,也不需要转换为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-01-29 06:59:36
试着像这样打印:
- (void) customPushHandler:(NSDictionary *)notification {
NSLog(@"notification:%@", notification);
NSLog(@"Moenage:%@", notification[@"app_extra"]);
NSLog(@"Menage apps:%@", notification[@"aps"]);
}https://stackoverflow.com/questions/35078341
复制相似问题