objective-c - 在 UITableView 中加载本地 json 信息

标签 objective-c json parsing uitableview nsdictionary

我有自己的 json 文件“list.json”,用于获取如下所示的示例信息列表。我的 json 文件是 位于 Xcode 内部,我想将我的信息显示到表格中,请您给我一些提示和帮助来实现此实现,我如何解析本地 json 并在表格中加载信息。

[
{
    "number": "1",
    "name": "jon"
},
{
    "number": "2",
    "name": "Anton"
},
{
    "number": "9",
    "name": "Lili"
},
{
    "number": "7",
    "name": "Kyle"
},
{
    "display_number": "8",
    "name": "Linda"
}
]

最佳答案

您可以创建一个继承自 UITableViewController 的自定义类。

将list.json文件的内容读入数组的代码是:

NSString * filePath =[[NSBundle mainBundle] pathForResource:@"list" ofType:@"json"];

    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];


    if(error)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }


    self.dataList = (NSArray *)[NSJSONSerialization
                                JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                options:0 error:NULL];

头文件是:

#import <UIKit/UIKit.h>

@interface TVNA_ReadingDataTVCViewController : UITableViewController

@end

实现是:

#import "TVNA_ReadingDataTVCViewController.h"

@interface TVNA_ReadingDataTVCViewController ()

@property  NSArray* dataList;

@end

@implementation TVNA_ReadingDataTVCViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    [self readDataFromFile];

    [self.tableView reloadData];
}


-(void)readDataFromFile
{
    NSString * filePath =[[NSBundle mainBundle] pathForResource:@"list" ofType:@"json"];

    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];


    if(error)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }


    self.dataList = (NSArray *)[NSJSONSerialization
                                JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                options:0 error:NULL];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    id keyValuePair =self.dataList[indexPath.row];

    cell.textLabel.text = keyValuePair[@"name"];

    cell.detailTextLabel.text=[NSString stringWithFormat:@"ID: %@", keyValuePair[@"number"]];
    return cell;
}


@end

最后,在您的 Storyboard上,将此类指定为您的 TableView Controller 的自定义类。希望这会有所帮助。

关于objective-c - 在 UITableView 中加载本地 json 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21776725/

相关文章:

objective-c - 在 myproject-swift.h 中找不到接口(interface)声明

javascript - 如何在 javascript 中添加 json 数组中的所有价格元素

json - PowerShell:包含特殊字符的ConvertTo-Json问题

json - 关于 EBNF 表示法和 JSON 的问题

c# - 当字符串似乎与模式匹配时,TryParseExact 返回 false

带有 pcap 的 Python dpkt - 如何打印数据包数据?

parsing - 解析树、注释解析树和激活树有什么区别?(编译器)

iphone - UITableView 编辑模式不工作

objective-c - UIScrollView:每个维度/轴的缩放比例

ios - 用两个字符串过滤字典数组