在ios中解析json数据
刚刚下午那会 弄了个 解析 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 , 指定顶级节点可以部署数组或字典
在ios中解析json数据的更多相关文章
- IOS中的JSON数据的解析
解析Json数据 //加载.json文件 NSString *path = [[NSBundle mainBundle]pathForResource:@"product.json" ...
- iOS中解析json多种方法
我感觉JSON解析,重要的是JSON解析之后对结果的处理JSON解析后是个dictionary,但是字典中有可能包含字典和数组,数组中还可以包含字典.向客户端请求的返回数据解析下面就简单介绍一下JSO ...
- Android中解析Json数据
在开发中常常会遇到解析json的问题 在这里总结几种解析的方式: 方式一: json数据: private String jsonData = "[{\"name\":\ ...
- iOS边练边学--iOS中的json数据解析
JSON数据(NSData) -> OC对照表 {} -> NSDictionary @{} [] -> NSArray @[] "jack" -> NSS ...
- ios中解析json对象基类
这个是对上面一篇写的一个解析json对象的基类 @interface BaseObjectFromJson : NSObject + (id) objectWithDict:(NSDictionary ...
- [转]在SqlServer 中解析JSON数据
在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server ...
- C#中解析JSON数据,并获取到其中的值
1.应需求创建一个Json字符串 string json = "[{'Name':'张三','age':'20','Data':{'ID':100,'heigh':'180','weight ...
- 在SqlServer 中解析JSON数据 [parseJSON] 函数 数据库中 解析JSON
使用如下: SELECT * FROM parseJSON('{ "联系人": { "姓名": "huang", "网名" ...
- 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)
在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...
随机推荐
- datazen Active Directory AD 配置
今天苦心经营的datazen 链接AD,文档已经无法吐槽了简单的几句话,根本不够用. 先说一下链接AD 的好处吧, 1 首先免去设置密码的麻烦,因为直接用AD账号的密码. 2 更安全,因为客户可不想自 ...
- [转]最详细的 HTTPS 科普扫盲帖
转载自http://www.codeceo.com/article/https-knowledge.html 为什么需要https HTTP是明文传输的,也就意味着,介于发送端.接收端中间的任意节点都 ...
- C# Double类型 不四舍五入
测试中发现Double类型需要#0.00 小数点精度为后2位,并且多余部分不需要四舍五入,直接截断 用字符串处理也可以,但是比较麻烦 这里给出一种思路: double a = 9999.999; a ...
- react服务端渲染(同构)
学习react也有一段时间了,使用react后首页渲染的速度与seo一直不理想.打算研究一下react神奇服务端渲染. react服务端渲染只能使用nodejs做服务端语言实现前后端同构,在后台对re ...
- sqlite3 小结
sqlite安装 DDL(数据定义语言):create.alter.drop DML(数据操作语言):insert.update.delete DQL(数据查询语言):select sqlite3 命 ...
- 错误:Duplicate interface definition for class
错误: Duplicate interface definition for class "类名称" 原因:在工程中有相同的文件或相同的 @interface 类名称 解决办法: ...
- windows8.1 下搭建配置apache+php+mysql
软件版本: apache:Apache 2.4.10 Win64 http://www.apachelounge.com/download/VC11/binaries/httpd-2.4.10- ...
- connect函数
TCP客户用connect函数来建立与TCP服务器的连接 int connect (int sockfd, const sockaddr * servaddr, socklen_t addrlen); ...
- TableView_编辑 实例代码
@interface MJViewController () <UITableViewDataSource, UITableViewDelegate> { NSMutableArray * ...
- iOS程序-UIScrollView的基本使用
iOS程序-UIScrollView的基本使用 scrollView的几个属性contentSize contentOffset contentInset 1.不能向上滑动很可能是因为contentS ...