2018-05-30 项目:JSON解析

作者: 肠粉白粥_Hoben | 来源:发表于2018-05-30 11:52 被阅读12次

一.项目要求

二.代码实现

这次项目中,我把JSON数据写入了一个txt文档中,读入后转换成NSString,再转换成NSData,最后把NSData转换成NSDictionary,完成该次项目。首先需要把这次项目的方法都封装成一个类:

#import <Foundation/Foundation.h>

@interface ParseJsonMethod : NSObject

@property (nonatomic, assign) NSDictionary *jsonDictionary;
@property (nonatomic, copy) NSMutableString *jsonString;
//读入文件
- (void) initWithJsonFile: (char *) path;
//解析JSON
- (void) parseWithJsonString;

@end

1.读入文件

-(void) initWithJsonFile:(char *)path
{
    //读入
    FILE *jsonFile = fopen(path, "r");
    char word[200];
    _jsonString = [NSMutableString stringWithCapacity: 50];
    while (fgets(word, 200, jsonFile)) {
        //获取文本内容
        NSString *tmp = [NSString stringWithUTF8String: word];
        [_jsonString appendString: tmp];
    }
}

2.解析JSON

本来想直接用暴力来解析NSString,但是还是上网搜了一下Cocoa库有没有现成的JSON解析方法,结果还真的被我发现了。
首先我们需要将NSString类型的json数据转换成NSData类型,然后获得了NSData后,如果data合法,则使用NSJSONSerialization的方法转换成相应的字典。
其中,option有以下三种参数:(参考https://www.jianshu.com/p/117256de5b95

  • NSJSONReadingMutableContainers = 转换出来的对象是可变数组或者可变字典
  • NSJSONReadingMutableLeaves = 转换呼出来的OC对象中的字符串是可变的\注意:iOS7之后无效 bug
  • NSJSONReadingAllowFragments = 如果服务器返回的JSON数据,不是标准的JSON,那么就必须使用这个值,否则无法解析
- (void) parseWithJsonString
{
    //将json数据解码
    NSData* jsonData;
    jsonData = [_jsonString dataUsingEncoding:NSUTF8StringEncoding];
    if (jsonData) {
        _jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData
                                                  options: NSJSONReadingMutableContainers
                                                               error: nil];
    }
}

3.输出结果

使用类别,将NSDictionary里面的中文都转码:

#import "NSDictionary.h"

@implementation NSDictionary(UniCode)

- (NSString*)my_description {
    NSString *desc = [self description];
    desc = [NSString stringWithCString:[desc cStringUsingEncoding:NSUTF8StringEncoding] 
                     encoding:NSNonLossyASCIIStringEncoding];
    return desc;
}

@end

在main.m里面调用我们封装好的方法:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "parseJsonMethod.h"
#import "NSDictionary.h"
int main(int argc, char * argv[]) {
    @autoreleasepool {
        char* path = "/Users/huanghongbin/Library/Mobile Documents/com~apple~TextEdit/Documents/jsonFile.txt";
        
        ParseJsonMethod *method = [[ParseJsonMethod alloc] init];
        //获取文本内容
        [method initWithJsonFile: path];
        //解析JSON数据
        [method parseWithJsonString];
        //输出获得的字典。
        //输出时中文转码错误,只能通过类别定义来返回NSString从而输出
        if ([method jsonDictionary])
            NSLog(@"The result dictionary is %@", [[method jsonDictionary] my_description]);
        else
            NSLog(@"JSON数据不合法!");
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

最后成功了!


valid结果.png invalid结果.png

三.踩过的坑

读取文件的时候,不小心照搬了书上的内容,将最后一位替换成了'\0',结果最后一行的括号没有读进去,导致json数据不对,dictionary死活都输出了null,以后要注意不能照搬,要多思考!

相关文章

  • 2018-05-30 项目:JSON解析

    一.项目要求 二.代码实现 这次项目中,我把JSON数据写入了一个txt文档中,读入后转换成NSString,再转...

  • json-1008

    json入门json实验json在项目中的应用总结 json练习题 json解析练习

  • 测试Json效率和XML效率

    tips:把项目中的解析json的sbjson ,网上提到的JsonKit和apple提供的自带json解析方法进...

  • Swift Json解析探索

    Swift Json解析探索 客户端开发项目中,不可避免地需要解析网络数据---将服务端下发的JSON数据解析成客...

  • JSON

    JSON解析(反序列化) JSON解析(序列化) JSON解析补充

  • Json解析方式

    1.传统的JSON解析 1.1 生成Json 1.2 解析Json 2.Gson解析Json 2.1生成Json ...

  • Python学好了,飞机票你还买不到么?

    项目:机票数据采集 使用模块:requests(请求模块),js2py(js执行模块),json(解析json),...

  • Flutter json_serializable 解析 JSO

    背景 目前我们项目中,解析 JSON 已经全部使用官方推荐的 json_serializable 方案,为了帮助团...

  • Gson源码解析和它的设计模式

    前言 之前一段时间,准备把糗百的项目中json解析的模块中的原生Json解析换成gson解析,工作比较繁杂,坑多,...

  • Gson源码解析和它的设计模式

    前言 之前一段时间,准备把糗百的项目中json解析的模块中的原生Json解析换成gson解析,工作比较繁杂,坑多,...

网友评论

    本文标题:2018-05-30 项目:JSON解析

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