IOS中JSON解析<代码演示>
2014-03-14 00:14
393 查看
/*
JSON文件使用键值对Key和Value的形式出现的
在IOS开发中使用JSON数据较多,一般使用的都是JSON解析数据比如一下一份JSON文件:
{
"students": [
{
"name":"张三",
"telephoneNumber":"1234567891",
"gender":"男"
},
{
"name":"李四",
"telephoneNumber":"1734995333",
"gender":"男"
}
]
}
解析步骤很简单单:
1.获取沙盒中JSON文件<说明:这里演示的是沙盒下的JSON解析,开发中通常使用的网络数据>
2.调用JSON解析器直接解析路径下的JSON文件存入字典中<Java中的Map集合>
*/
/*-----------------使用JSON解析------------------*/
//1.获取JSON文件
NSString *jsonString = [NSString
stringWithContentsOfFile:[[NSBundle
mainBundle] pathForResource:@"students"
ofType:@"json"]encoding:NSUTF8StringEncoding
error:nil];
//2.实例化JSON解析器
NSDictionary *jsonSerial = [NSJSONSerialization
JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:nil];
NSArray *value = [jsonSerial
objectForKey:@"students"];
self.stuArray = [NSMutableArray
array];
for (int i=0; i<value.count; i++) {
NSDictionary *dic = [value
objectAtIndex:i];
Students *stus = [[Students
alloc] init];
stus.name = [dic
objectForKey:@"name"];
stus.telephoneNumber = [dic
objectForKey:@"telephoneNumber"];
stus.gender = [dic
objectForKey:@"gender"];
[self.stuArray
addObject:stus];
[stus
release],stus = nil;
}
JSON文件使用键值对Key和Value的形式出现的
在IOS开发中使用JSON数据较多,一般使用的都是JSON解析数据比如一下一份JSON文件:
{
"students": [
{
"name":"张三",
"telephoneNumber":"1234567891",
"gender":"男"
},
{
"name":"李四",
"telephoneNumber":"1734995333",
"gender":"男"
}
]
}
解析步骤很简单单:
1.获取沙盒中JSON文件<说明:这里演示的是沙盒下的JSON解析,开发中通常使用的网络数据>
2.调用JSON解析器直接解析路径下的JSON文件存入字典中<Java中的Map集合>
*/
/*-----------------使用JSON解析------------------*/
//1.获取JSON文件
NSString *jsonString = [NSString
stringWithContentsOfFile:[[NSBundle
mainBundle] pathForResource:@"students"
ofType:@"json"]encoding:NSUTF8StringEncoding
error:nil];
//2.实例化JSON解析器
NSDictionary *jsonSerial = [NSJSONSerialization
JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:nil];
NSArray *value = [jsonSerial
objectForKey:@"students"];
self.stuArray = [NSMutableArray
array];
for (int i=0; i<value.count; i++) {
NSDictionary *dic = [value
objectAtIndex:i];
Students *stus = [[Students
alloc] init];
stus.name = [dic
objectForKey:@"name"];
stus.telephoneNumber = [dic
objectForKey:@"telephoneNumber"];
stus.gender = [dic
objectForKey:@"gender"];
[self.stuArray
addObject:stus];
[stus
release],stus = nil;
}
相关文章推荐
- IOS-CoreData简单使用的主要代码<代码演示>
- IOS中归档和反归档操作<代码演示>
- ios XML,JSON,数组解析并转换成NSMutableArray(List<class>)
- iOS json解析遇到@"";@"<null>";null 处理
- IOS简单地图效果<代码演示>
- IOS中Notificaiton通知的使用方法<代码演示>
- IOS视图切换动画效果<代码演示>
- asp.net ajax解析json数据(运用List<Hashtable>装数据)
- Cocos2d-x控件使用CCControlSlider<代码演示>
- <iOS>NSXMLParser具体解析xml的应用详解
- Ajax 上传文件返回JSON字符串FireFox解析出来后带有<pre>标签解决办法
- Cocos2d-x控件使用CCControlSwitch<代码演示>
- <<Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法>>笔记-块与大中枢派发
- <<精通iOS开发>>第14章例子代码小缺陷的修复
- json解析尖括号<>
- PHP中遇到BOM、<feff>编码导致json_decode函数无法解析问题
- <<精通iOS开发>>第14章例子代码小缺陷的修复
- Google Dexmaker 代码解析(四)-- MethodId<D, R>
- cocos2d-x控件使用CCControlButton<代码演示>
- <<精通iOS开发>>第14章例子代码小缺陷的修复