iOS我爱编程

iOS中的json和xml解析

2017-05-02  本文已影响92人  有缘人2830

效果图


42E26B0A-C30A-4C06-A649-0900AFA9834F.png

一、本文章主要介绍 JSON XML两种数据解析方式
1.JSON 包括系统方法的JSON ,以及三方JSONKit
2.XML 系统的解析(SAX方式),三方google的一个解析方式(DOM解析)
SAX 解析: 这是苹果系统自带的xml解析方法,特点就是:逐行解析, 这样方式效率低,但是可以快速的判断定位到哪一行出错。
DOM解析:全局解析,效率高,但是不容易定位到出错的位置。
二、JSON 解析
《2.1》首先创建一个 json文件,eg: Student.json
[
{
"name":"张三",
"age":"18",
"sex":"男"
},
{
"name":"Jones",
"age":"20",
"sex":"女"
},
{
"name":"Tom",
"age":"22",
"sex":"男"
}
]
《2.2》 定义一个model类 StuJsonModel
StuJsonModel.h 文件

import <Foundation/Foundation.h>

@interface StuJsonModel : NSObject

/****** model 属性 ******/
@property (copy, nonatomic) NSString *name, *age, *sex;

//方法

@end

StuJsonModel.m 文件中

import "StuJsonModel.h"

@implementation StuJsonModel

//描述内容

}

//方法

}
@end
《2.3》 系统JSON -- 按钮里面内容
//1.首先获取json路径 【这里是指本地的json文件】
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];

//2.转化为二进制data类型
NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];

//3.Student.json 最外圈是数组  用数组还是字典根据最外面的数据形式,这里是数组形式
NSError *error = nil;
 NSMutableArray *studentArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

//4.输出数据
if (error == nil) {
    NSLog(@"%@",studentArray);
}else{
    NSLog(@"%@",error);
}

//5.封装model数据
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
for (NSDictionary *dic in studentArray) {
    StuJsonModel *model = [StuJsonModel new];
    [model getJSONModel:dic isKVC:YES];
    [arr addObject:model];
}
NSLog(@"%@",arr);

//6. 取数据
for (int i = 0; i < arr.count; i ++) {
    StuJsonModel *model = arr[i];
    NSLog(@"%@",model.sex);
}

《2.4》JSONKit -- 按钮里面内容 git下载JSONKit文件
//注意:JSONKit是mrc环境 需要更改设置 -fno-objc-arc

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

//2.获取json内容
NSString *jsonString = [[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//3.JSONkit解析 用数组还是字典根据最外面的数据形式,这里是数组形式
NSMutableArray *arr = [jsonString objectFromJSONString];

//4. 数据封装
NSMutableArray *arr2 = [NSMutableArray array];
for (NSDictionary *dic in arr) {
    StuJsonModel *model = [StuJsonModel new];
    [model getJSONModel:dic isKVC:NO];
    [arr2 addObject:model];
}
NSLog(@"%@",arr2);

注:XMLDictionary 一个xml的解析库

三、XML解析
《3.1》首先创建一个 xml文件,eg: Teacher.xml
<?xml version="1.0" encoding="UTF-8"?>
<teachers>
<teacher>
<name>ZhangSan</name>
<age>18</age>
<phone>188</phone>
</teacher>
<teacher>
<name>LiSi</name>
<age>17</age>
<phone>178</phone>
</teacher>
<teacher>
<name>Tom</name>
<age>28</age>
<phone>156</phone>
</teacher>
</teachers>

《3.2》建一个TeaXMLModel
.h文件

import <Foundation/Foundation.h>

@interface TeaXMLModel : NSObject

/****** 老师 属性 ******/
@property (copy, nonatomic) NSString *name, *age, *phone;

@end

.m文件

import "TeaXMLModel.h"

@implementation TeaXMLModel

@end

《3.3》系统的XML 解析 导入NSXMLParserDelegate 代理
新建一个数组 并初始化
/****** mutarray ******/
@property (strong, nonatomic) NSMutableArray *teaArray;

NSString *filePath =  [[NSBundle mainBundle] pathForResource:@"Teacher" ofType:@"xml"];

NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath];
NSXMLParser *par = [[NSXMLParser alloc]initWithContentsOfURL:url];
par.delegate = self;
[par parse];

//2.-- NSXMLParserDelegate 代理方法

pragma mark -- NSXMLParserDelegate

// self.teaArray = [NSMutableArray array];

}

}

《3.4》GDataXMLNode 解析 git下载文件包托进去
使用GDataXMLNode 需要注意一下三点:
// 1> libxml includes require that the target Header Search Paths contain 添加 /usr/include/libxml2
// 2> and Other Linker Flags contain 添加 -lxml2
// 3> 导入 libxml2.tbd 库 IOS>7.0 .tbd IOS<7.0 .dylib

//1.获取文件路径

NSString *filePath =  [[NSBundle mainBundle] pathForResource:@"Teacher" ofType:@"xml"];

//2.转化data
NSData *xmlData = [[NSData alloc]initWithContentsOfFile:filePath];

//3.根据data初始化 GDataXMLDocument
GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];

//4.获取根节点 teachers
GDataXMLElement *rootElement = [doc rootElement];

//5.查找子节点 teacher
NSArray *teachers = [rootElement elementsForName:@"teacher"];

for (GDataXMLElement *tech in teachers) {
    TeaXMLModel *model = [TeaXMLModel new];
    
    //1.获取name节点
    model.name = [[[tech elementsForName:@"name"] firstObject] stringValue];
    //或者 model.name = [[[tech elementsForName:@"name"] objectAtIndex:0] stringValue];
    //2.获取age节点
    model.age = [[[tech elementsForName:@"age"] firstObject]stringValue];
    //3.获取phone节点
    model.phone = [[[tech elementsForName:@"phone"] firstObject]stringValue];
    
    //添加到数组
    [self.teaArray addObject:model];
}

NSLog(@"self.teaArray:%@",self.teaArray);

四、总结,个人认为三方固然好用,节省了开发时间,但是一旦出问题,还是需要比较久的时间去处理bug. 在使用JSONKit的时候,因为创建的json文件最后一个数组后面多了一个 “,”逗号,JSONKit就不能解析了,为nil。而系统的就可以解析。所以做开发还是要细心、细心、细心!!! 重要的事情说3遍!!! 马化腾 284255853
传送门 http://code.cocoachina.com/view/134937

上一篇 下一篇

猜你喜欢

热点阅读