ios json数据解析
2015-07-01 13:36
393 查看
转自:
http://blog.csdn.net/chonbj/article/details/25982511
刚刚下午那会 弄了个 解析 xml demo的小例子,本想着json也挺复杂 弄还是 不弄,但是简单的看了下 发现挺简单,这个时候就犹豫了,要不写到博客上来 ?
考虑了很久,还是写上来吧,毕竟json用得太多了,而且算是自己的积累吧,毕竟刚开始学习IOS开发方面的知识,就当是巩固了撒!
还是 先看个效果图吧,如下!
接下来 看下工程目录吧,其实并没有必要,直接建立一个工程就行 ,算了,还是贴上来吧,如下:
工程目录中有个 Notes.json 文件,该文件就是 要解析的json数据了 ,也截下图吧,如下:
Ok ,以上准备完毕,就开始编码了,在此之前故事版的内容 就和我上篇博客文章 IOS 解析xml 故事版 是一样配置的,这里就不在啰嗦了 ,首先看下 chonViewController.h文件,代码如下:
[objc] view
plaincopy
//
// chonViewController.h
// TestJson
//
// Created by choni on 14-5-16.
// Copyright (c) 2014年 choni. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface chonViewController : UITableViewController
//保存数据列表
@property(nonatomic,strong) NSMutableArray * listData;
@end
与之对应的 chonViewController.m文件 代码如下:
[objc] view
plaincopy
//
// chonViewController.m
// TestJson
//
// Created by choni on 14-5-16.
// Copyright (c) 2014年 choni. All rights reserved.
//
#import "chonViewController.h"
@interface chonViewController ()
@end
@implementation chonViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString * path = [[NSBundle mainBundle]pathForResource:@"Notes" ofType:@"json" ];
NSData * jsonData = [[NSData alloc] initWithContentsOfFile:path];
NSError * error ;
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (!jsonObj || error) {
NSLog(@"JSON解析失败");
}
self.listData = [jsonObj objectForKey:@"Record"];
}
#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.listData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSMutableDictionary * dict = self.listData[indexPath.row];
cell.textLabel.text = [dict objectForKey:@"Content"];
cell.detailTextLabel.text = [dict objectForKey:@"CDate"];
return cell ;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
Ok , 现在就可以编译运行的程序了 ,但是有个主意的地方 :
1.因为使用 NSJSONSerialization 实现json解码,要确定你得项目使用IOS 5 SDK 才可以
2. 其他的就没有什么了,介绍下NSJSONSerialization的类方法吧
1)NSJSONReadingMutableContaines ,指定解析返回的是可变的数组或字典 ,这个方法还是比较使用的,因为如果json数据需要改,不用管撒
2)NSJSONReadingMutableLeaves ,指定叶节点是可变的字符串
3) NSJSONReadingAllowFragments , 指定顶级节点可以部署数组或字典
另,json是轻量级的数据交换格式。。。。什么的,去你大爷的,但在应用开发的时候 优选考虑json 而不是xml ,xml 最先考虑的应当是TBXML,上篇博客提到过,json 最先
考虑NSJSONSerialization 。
http://blog.csdn.net/chonbj/article/details/25982511
刚刚下午那会 弄了个 解析 xml demo的小例子,本想着json也挺复杂 弄还是 不弄,但是简单的看了下 发现挺简单,这个时候就犹豫了,要不写到博客上来 ?
考虑了很久,还是写上来吧,毕竟json用得太多了,而且算是自己的积累吧,毕竟刚开始学习IOS开发方面的知识,就当是巩固了撒!
还是 先看个效果图吧,如下!
接下来 看下工程目录吧,其实并没有必要,直接建立一个工程就行 ,算了,还是贴上来吧,如下:
工程目录中有个 Notes.json 文件,该文件就是 要解析的json数据了 ,也截下图吧,如下:
Ok ,以上准备完毕,就开始编码了,在此之前故事版的内容 就和我上篇博客文章 IOS 解析xml 故事版 是一样配置的,这里就不在啰嗦了 ,首先看下 chonViewController.h文件,代码如下:
[objc] view
plaincopy
//
// chonViewController.h
// TestJson
//
// Created by choni on 14-5-16.
// Copyright (c) 2014年 choni. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface chonViewController : UITableViewController
//保存数据列表
@property(nonatomic,strong) NSMutableArray * listData;
@end
与之对应的 chonViewController.m文件 代码如下:
[objc] view
plaincopy
//
// chonViewController.m
// TestJson
//
// Created by choni on 14-5-16.
// Copyright (c) 2014年 choni. All rights reserved.
//
#import "chonViewController.h"
@interface chonViewController ()
@end
@implementation chonViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString * path = [[NSBundle mainBundle]pathForResource:@"Notes" ofType:@"json" ];
NSData * jsonData = [[NSData alloc] initWithContentsOfFile:path];
NSError * error ;
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (!jsonObj || error) {
NSLog(@"JSON解析失败");
}
self.listData = [jsonObj objectForKey:@"Record"];
}
#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.listData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSMutableDictionary * dict = self.listData[indexPath.row];
cell.textLabel.text = [dict objectForKey:@"Content"];
cell.detailTextLabel.text = [dict objectForKey:@"CDate"];
return cell ;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
Ok , 现在就可以编译运行的程序了 ,但是有个主意的地方 :
1.因为使用 NSJSONSerialization 实现json解码,要确定你得项目使用IOS 5 SDK 才可以
2. 其他的就没有什么了,介绍下NSJSONSerialization的类方法吧
1)NSJSONReadingMutableContaines ,指定解析返回的是可变的数组或字典 ,这个方法还是比较使用的,因为如果json数据需要改,不用管撒
2)NSJSONReadingMutableLeaves ,指定叶节点是可变的字符串
3) NSJSONReadingAllowFragments , 指定顶级节点可以部署数组或字典
另,json是轻量级的数据交换格式。。。。什么的,去你大爷的,但在应用开发的时候 优选考虑json 而不是xml ,xml 最先考虑的应当是TBXML,上篇博客提到过,json 最先
考虑NSJSONSerialization 。
相关文章推荐
- iOS 某些特殊名词的意思
- 持续更新中 --- iOS移动开发中的优质资源
- 手把手教你ARC——iOS/Mac开发ARC入门和使用
- iOS开发ARC内存管理技术要点
- iOS事件全面解析 (触摸事件、手势识别、摇晃事件、耳机线控)
- iOS 导航相关的特殊设置
- iOS 测试指南
- IOS开发---获取用户当前地理坐标
- 使用cocoapods来管理iOS工程中的第三方类库
- iOS中的block和retain cycle
- iOS-XIB错误,找不到方法:Could not load NIB in bundle
- iOS自动订阅商品(Auto-Renewable Subscriptions)相关处理
- IOS自带Email的两种方法
- iOS Cocoa框架介绍
- [iOS]创建单一像素的分割线
- iOStableviewcell系统自带删除注意事项
- 汇总iOS开发中需要用到的开源库
- 10个必需的iOS开发工具和资源
- 总结分享10大iOS开发者最喜爱的库
- iOS开发 关于SEL的简单总结