如何在表格视图中显示嵌套的JSON数据?
我JSON这样如何在表格视图中显示嵌套的JSON数据?
{ "StartElement": {
"Count": "14",
"Notification": [
{
"contact": null,
"Date": "20 June 2016",
"Message": null,
"Viewed": "1"
},
{
"contact": "99230332210",
"Date": "20 June 2016",
"Message": "I need help",
"Viewed": "1"
}
}
和我的JSON在viewDidLoad
分析是这样的:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.someURL.com/JSONToFetch?"]];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
NSLog(@"code: %d",[(NSHTTPURLResponse*)response statusCode]);
}] resume];
现在,我想告诉"Message"
内容转换成表格视图单元。如何将此数据存储到数组中,以便我可以将它加载到cellForRowAtIndexPath
方法中。感谢名单为帮助:)
如果你想获得一个特定的键值,那么你必须在字典中转换响应格式和转换后,你可以得到的特定键
值转换,请这样
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.someURL.com/JSONToFetch?"]];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSError *error;
NSDictionary *requestReply=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];//use Reading option NSJSONReadingAllowFragments because response contain array
NSLog(@"requestReply: %@", requestReply);
NSLog(@"code: %d",[(NSHTTPURLResponse*)response statusCode]);
NSArray *notification_list=[[requestReply valueForKey:@"StartElement"] valueForKey:@"Notification"];
}] resume];
我试过,但'notification_list'仍然是空感谢,u能plz帮助我获得所有'“消息”'到一个数组 –
您的JSON是无效它不包含闭幕阵列 –
是的,我纠正了 –
您可以保存您的回复在字典
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
从这个字典可以提取您的信息的NSArray,并用它到表
是的,我用这个,您的帮助:) –
我想你的JSON解析是错误,你应该解析你的JSON到一个NSDictionary,并通知到一个NSArray.I建议你使用第三方JSON解析库
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/你的反应首先阅读。 – Joshua