刚刚下午那会 弄了个 解析 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文件 代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片

//

//  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 , 指定顶级节点可以部署数组或字典

最新文章

  1. Java 二叉树遍历右视图-LeetCode199
  2. iOS状态栏---学习笔记六
  3. RAM和ROM总结
  4. zjtd 2016面试
  5. 【Linux常用工具】03. Linux性能测试工具ab
  6. apache开源项目--Sirona
  7. db4o官方停止支持及面向对象数据库的一些感想
  8. vs在winform中不给力哈-错误不提示
  9. eclipse开发c++时cout和endl报错
  10. 让textarea和附近的文字居中对齐
  11. ASP.NET Core 一步步搭建个人网站(2)_一键部署和用户注册登录
  12. 策略模式(Stratety)
  13. lodop打印收费小票
  14. service order
  15. Event Loop浅谈
  16. python 搭建一个http服务的小例子
  17. VMware虚拟机网络设置
  18. Ckeditor一种很方便的文本编辑器
  19. storm的trident编程模型
  20. 001.LVM简介

热门文章

  1. Electron
  2. maven自动部署到tomcat的问题
  3. iOS学习笔记(十四)——打电话、发短信
  4. Xcode换版本或者改名字后无法使用simpholders2
  5. hibernate_validator_08
  6. 在VC6中使用ogre进行游戏开发
  7. 【USACO 3.1.3】丑数
  8. Mybatis3.0防止SQL注入
  9. 获取fragment中对应的控件的写法
  10. mybatis中association的column传入多个参数值