美文网首页
iOS 开发之解析Json文件

iOS 开发之解析Json文件

作者: KODIE | 来源:发表于2018-02-09 16:03 被阅读0次

需求:

本文内容很简单,但是做一个记录,如何解析Json文件。

步骤:

①第一步:先按照Json格式准备一段Json串,如下:

[
    {
        "timeIndex": 24,
        "text": "时辰不清楚"
    }, 
    {
        "timeIndex": 23,
        "text": "子时 23点"
    }, 
    {
        "timeIndex": 0,
        "text": "子时 0点"
    }, 
    {
        "timeIndex": 1,
        "text": "丑时 1点"
    }, 
    {
        "timeIndex": 2,
        "text": "丑时 2点"
    }, 
    {
        "timeIndex": 3,
        "text": "寅时 3点"
    }, 
    {
        "timeIndex": 4,
        "text": "寅时 4点"
    }, 
    {
        "timeIndex": 5,
        "text": "卯时 5点"
    }, 
    {
        "timeIndex": 6,
        "text": "卯时 6点"
    }, 
    {
        "timeIndex": 7,
        "text": "辰时 7点"
    }, 
    {
        "timeIndex": 8,
        "text": "辰时 8点"
    }, 
    {
        "timeIndex": 9,
        "text": "巳时 9点"
    }, 
    {
        "timeIndex": 10,
        "text": "巳时 10点"
    }, 
    {
        "timeIndex": 11,
        "text": "午时 11点"
    }, 
    {
        "timeIndex": 12,
        "text": "午时 12点"
    }, 
    {
        "timeIndex": 13,
        "text": "未时 13点"
    }, 
    {
        "timeIndex": 14,
        "text": "未时 14点"
    }, 
    {
        "timeIndex": 15,
        "text": "申时 15点"
    }, 
    {
        "timeIndex": 16,
        "text": "申时 16点"
    }, 
    {
        "timeIndex": 17,
        "text": "酉时 17点"
    }, 
    {
        "timeIndex": 18,
        "text": "酉时 18点"
    }, 
    {
        "timeIndex": 19,
        "text": "戌时 19点"
    }, 
    {
        "timeIndex": 20,
        "text": "戌时 20点"
    }, 
    {
        "timeIndex": 21,
        "text": "亥时 21点"
    }, 
    {
        "timeIndex": 22,
        "text": "亥时 22点"
    }
]

这个可以在网上搜一些校验Json格式的平台进行校验一下,没有问题之后就可以放在json.txt或者json.json的文件中,然后拖入到工程中:


Snip20180209_180.png

②第二步:就是解析json文件了,在需要加载json文件的地方:

- (void)loadData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"json.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error;
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
    NSLog(@"%@",dataArray);
    //自己进行字典转模型,此处省略
    
    //这里进行tableView的刷新操作
    [self.tableView reloadData];
}

好吧,记录内容就到这里了,好吧,很简单,非常简单,写不下去了,以上!!!


蓝瘦.gif

相关文章

网友评论

      本文标题:iOS 开发之解析Json文件

      本文链接:https://www.haomeiwen.com/subject/lkzftftx.html