-1

我在解析目标 C 中的 JSON 字符串时遇到问题:我的 JSON:

{"messages":[{"nick":"Tim","message":"Hallo","time":"06.07.2012 13:26:41"}]}

我的目标 C 代码:

NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:@"..URL.."];
NSArray *messages = [data objectForKey:@"messages"];
NSDictionary *json = [NSJSONSerialization
                           JSONObjectWithData:messages
                                      options:NSJSONReadingMutableLeaves
                                        error:&error];
NSString *nick = [json objectForKey:@"nick"];
NSString *message = [json objectForKey:@"message"];

但这不起作用,我不知道该怎么办!

4

1 回答 1

2

您的 JSON 是字典数组的字典,即 {[{}]}

NSArray *messages = [json objectForKey:@"messages"];
NSString* nick = [[messages objectAtIndex:0]objectForKey:@"nick"]
于 2012-07-06T13:00:39.423 回答