ios json数据解析
来源:互联网 发布:软件图标更换器 编辑:程序博客网 时间:2024/06/04 17:51
转自:
http://blog.csdn.net/chonbj/article/details/25982511
刚刚下午那会 弄了个 解析 xml demo的小例子,本想着json也挺复杂 弄还是 不弄,但是简单的看了下 发现挺简单,这个时候就犹豫了,要不写到博客上来 ?
考虑了很久,还是写上来吧,毕竟json用得太多了,而且算是自己的积累吧,毕竟刚开始学习IOS开发方面的知识,就当是巩固了撒!
还是 先看个效果图吧,如下!
接下来 看下工程目录吧,其实并没有必要,直接建立一个工程就行 ,算了,还是贴上来吧,如下:
工程目录中有个 Notes.json 文件,该文件就是 要解析的json数据了 ,也截下图吧,如下:
Ok ,以上准备完毕,就开始编码了,在此之前故事版的内容 就和我上篇博客文章 IOS 解析xml 故事版 是一样配置的,这里就不在啰嗦了 ,首先看下 chonViewController.h文件,代码如下:
- //
- // 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文件 代码如下:
- //
- // 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解析Json数据
- IOS-----> Json数据解析
- ios json数据解析
- iOS json数据解析
- ios json数据解析
- iOS解析json数据
- iOS JSON数据解析
- iOS中JSON数据解析
- ios 解析不规则json数据
- IOS数据解析之json
- iOS数据解析之json
- IOS中JSON数据解析
- iOS 解析不规则json数据
- iOS 数据解析 JSON XML
- iOS之 json数据解析
- IOS JSON数据解析(一)
- iOS KVC解析Json数据
- iOS中解析json数据
- Rust中文翻译8
- Shortest Palindrome leetcode-java
- MySQL 自增列插入0值的方法
- Shell脚本之数组
- rich:pickList标签的使用
- ios json数据解析
- 【Java基础学习笔记】输入输出流之Reader
- Android xxx is not translated in zh解决办法
- Assert()
- 8天学通MongoDB——第五天 主从复制
- 多态
- 混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- 零基础学习hadoop
- 索引器的一点点认识