将从 PubNub 收到的消息转换为 Dictionary 对象
Convert message received from PubNub to Dictionary object
我有以下用于接收 PubNub 消息的对象 C 代码。
- (void)client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {
NSLog(@"Received message: %@ on channel %@ at %@", message.data.message,
message.data.subscribedChannel, message.data.timetoken);
}
返回的数据为
Received message: (
{
key = userName;
value = Enoch;
},
{
key = photoID;
value = 3;
},
{
key = userID;
value = 1;
},
{
key = actionType;
value = chat;
},
{
key = message;
value = H;
}
) on channel chat at 14888810882049989
我想将消息解析为字典对象,以便使用 "key"
访问 "value"
我是 objective C 编程新手,不知道该怎么做。
请帮忙。
遍历消息数组并在字典中设置键值。
NSArray *array = (NSArray*)message.data.message;
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
for (NSDictionary *item in array) {
[dic setObject:[item objectForKey:@"value"] forKey:[item objectForKey:@"key"]];
}
NSLog(@"%@", dic);
或
NSArray *array = (NSArray*)message.data.message;
NSArray *values = [array valueForKey: @"value"];
NSArray *keys = [array valueForKey: @"key"];
NSDictionary *dic = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
NSLog(@"%@", dic);
您可以使用以下方法解析数据并将其转换为字典
ChatterBoxMessage *chatterBoxMessage = [[ChatterBoxMessage alloc] initFromDictionary: message.data.message withTimeToken: message.data.timetoken];
[chatterBoxMessage asDictionary];
通过这种方法你会得到dictionary
.
ChatterBoxMessage 上面是一个 PubNub 库 class。
您也可以像下面这样解析您的数据:
for (NSDictionary *objectData in message.data.message) {
NSLog(@"Value : %@",objectData[@"value"]);
NSLog(@"Key : %@",objectData[@"key"]);
}
我有以下用于接收 PubNub 消息的对象 C 代码。
- (void)client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {
NSLog(@"Received message: %@ on channel %@ at %@", message.data.message,
message.data.subscribedChannel, message.data.timetoken);
}
返回的数据为
Received message: (
{
key = userName;
value = Enoch;
},
{
key = photoID;
value = 3;
},
{
key = userID;
value = 1;
},
{
key = actionType;
value = chat;
},
{
key = message;
value = H;
}
) on channel chat at 14888810882049989
我想将消息解析为字典对象,以便使用 "key"
访问 "value"我是 objective C 编程新手,不知道该怎么做。
请帮忙。
遍历消息数组并在字典中设置键值。
NSArray *array = (NSArray*)message.data.message;
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
for (NSDictionary *item in array) {
[dic setObject:[item objectForKey:@"value"] forKey:[item objectForKey:@"key"]];
}
NSLog(@"%@", dic);
或
NSArray *array = (NSArray*)message.data.message;
NSArray *values = [array valueForKey: @"value"];
NSArray *keys = [array valueForKey: @"key"];
NSDictionary *dic = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
NSLog(@"%@", dic);
您可以使用以下方法解析数据并将其转换为字典
ChatterBoxMessage *chatterBoxMessage = [[ChatterBoxMessage alloc] initFromDictionary: message.data.message withTimeToken: message.data.timetoken];
[chatterBoxMessage asDictionary];
通过这种方法你会得到dictionary
.
ChatterBoxMessage 上面是一个 PubNub 库 class。
您也可以像下面这样解析您的数据:
for (NSDictionary *objectData in message.data.message) {
NSLog(@"Value : %@",objectData[@"value"]);
NSLog(@"Key : %@",objectData[@"key"]);
}