【问题标题】:How to show nested JSON data in table view?如何在表格视图中显示嵌套的 JSON 数据?
【发布时间】:2016-06-21 08:59:03
【问题描述】:

我有这样的 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"
          }
     }

我在 viewDidLoad 中的 JSON 解析是这样的:

 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 方法中。感谢您的帮助:)

【问题讨论】:

标签: ios objective-c json


【解决方案1】:

如果您想获取特定键的值,则必须将响应转换为字典格式,转换后您可以获得特定键的值

请像这样转换您的回复

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 仍然为空,你能帮我把所有"Message" 放入一个数组中吗
  • 您的 json 无效,它不包含关闭数组
  • { "StartElement": { "Count": "14", "Notification": [ { "contact": null, "Date": "20 June 2016", "Message": null, “已查看”:“1”},{“联系人”:“99230332210”,“日期”:“2016 年 6 月 20 日”,“消息”:“我需要帮助”,“已查看”:“1”}] } }
【解决方案2】:

您可以将您的回复保存在字典中

 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

从这本字典中,您可以将消息提取到 NSarray 中并将其用于您的表中

【讨论】:

  • 是的,我用过这个,谢谢你的帮助:)
【解决方案3】:

我认为你的 JSON 解析是错误的,你应该把你的 JSON 解析成一个 NSDictionary,Notification 到一个 NSArray。我建议你使用第三方 JSON 解析库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多