• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用 Objective-C 到 UITableView 的 JSON 数据

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:01:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

-- 主 JSON 文件的更新问题 --

我正在尝试将名称添加到表格 View 中。但 table 总是空的。 (Xcode 7.1b1,iOS 9.1b2) 这只是我正在测试我的代码的示例 .json 文件。主 json 文件有更多的数据。

我成功地添加了示例 json 文件中的数据。但是现在使用主json文件后,表又是空的了。

.json 文件

[
  {
    "150978": {
      "jobs": {
        "D0-C14008-CW": {
          "05-9032": {
            "G-05": {
              "hours_rwk": "False",
              "hours_st": "4L",
              "hours_ewo_id": "D0-C14008-CWddda",
              "hours_ewo": "True",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          },
          "05-9031": {
            "B-02": {
              "hours_rwk": "False",
              "hours_st": "4L",
              "hours_ewo_id": "",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          }
        }
      },
      "name": "CirilloBetancourt",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "",
      "signed_in": "True",
      "time_in": "08: 30",
      "time_out": "16: 30",
      "signed_out": "True"
    },
    "151243": {
      "jobs": {},
      "name": "JasonDiaz",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "151248": {
      "jobs": {},
      "name": "AngelTorresMadrigal",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "151336": {
      "jobs": {},
      "name": "DanielFrey",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "151419": {
      "jobs": {
        "D0-C14009-CW1": {
          "05-9033": {
            "G-07": {
              "hours_rwk": "False",
              "hours_st": "8L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          }
        },
        "D0-C14008-CW": {
          "05-9031": {
            "B-02": {
              "hours_rwk": "False",
              "hours_st": "0L",
              "hours_ewo_id": "0",
              "hours_ewo": "True",
              "hours_dt": "0L",
              "hours_ot": "2L",
              "comments": ""
            }
          }
        }
      },
      "name": "MikeGullan",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9990666": {
      "jobs": {},
      "name": "MattStaricha",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9992610": {
      "jobs": {},
      "name": "MatthewBigelow",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9992778": {
      "jobs": {},
      "name": "ZhouTan",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9993273": {
      "jobs": {},
      "name": "RandyFarinelli",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996467": {
      "jobs": {},
      "name": "AlvaroSandoval(Guardado)",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996763": {
      "jobs": {},
      "name": "JesusZarate",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996767": {
      "jobs": {},
      "name": "JorgeRomero",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996768": {
      "jobs": {
        "D0-C14008-CW": {
          "05-9032": {
            "G-05": {
              "hours_rwk": "False",
              "hours_st": "3L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          },
          "05-9031": {
            "B-02": {
              "hours_rwk": "False",
              "hours_st": "5L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          }
        }
      },
      "name": "VictorRuiz",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996771": {
      "jobs": {},
      "name": "GabinoMata-Neri",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9999247": {
      "jobs": {
        "D0-C14008-CW": {
          "05-9032": {
            "G-05": {
              "hours_rwk": "False",
              "hours_st": "8L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          },
          "05-9031": {
            "B-02": {
              "hours_rwk": "True",
              "hours_st": "0L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "1L",
              "comments": ""
            }
          }
        }
      },
      "name": "FranciscoMontez",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9999314": {
      "jobs": {},
      "name": "RomanHernandez",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    }
  }
]

.h 文件

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@property (nonatomic, strong) NSArray *nameArray;
@property (nonatomic, strong) NSArray *dict;
@property (nonatomic, strong) NSData *data;
@property (nonatomic, strong) NSString *filePath;

@end

.m 文件

#import "TableViewController.h"
#import "TableViewCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    NSError *error = nil;

    _filePath = [[NSBundle mainBundle] pathForResource"data" ofType"json"];
    _data = [NSData dataWithContentsOfFile:_filePath];
    _dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return _dict.count;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier"TableCell" forIndexPath:indexPath];

    // Configure the cell...
    NSDictionary *dict = _dict[indexPath.row];
NSString *name = dict[@"name"];
cell.titleLabel.text = name;

    return cell;
}



Best Answer-推荐答案


您的 JSON 实际上格式错误。应该是:

[
    {
        "name": "XYZ",
        "details": "XYZ"
    },
    {
        "name": "XYZ",
        "details": "XYZ"
    }
]

当您执行 _dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error]; 时,您实际得到的是字典数组,不是单个字典。所以调用 _nameArray = [_dict objectForKey"name"]; 实际上应该返回 nil 或崩溃。

你的实现应该是这样的:

#import "TableViewController.h"
#import "TableViewCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *error = nil;
    _filePath = [[NSBundle mainBundle] pathForResource"data" ofType"json"];
    _data = [NSData dataWithContentsOfFile:_filePath];
    // Remember here, __dict is an array, I kept the same name as your code but it is NOT a dictionary.
    _dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error];
    _nameArray = [_dict objectForKey"name"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    return _dict.count;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier"TableCell" forIndexPath:indexPath];
    // Remember here, __dict is an array, I kept the same name as your code but it is NOT a dictionary.
    NSDictionary *dict = _dict[indexPath.row];
    NSString *name = dict[@"name"];
    cell.titleLabel.text = name;
    return cell;
}

关于ios - 使用 Objective-C 到 UITableView 的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32693193/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap