刚刚下午那会 弄了个 解析 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数据的更多相关文章

  1. IOS中的JSON数据的解析

    解析Json数据 //加载.json文件 NSString *path = [[NSBundle mainBundle]pathForResource:@"product.json" ...

  2. iOS中解析json多种方法

    我感觉JSON解析,重要的是JSON解析之后对结果的处理JSON解析后是个dictionary,但是字典中有可能包含字典和数组,数组中还可以包含字典.向客户端请求的返回数据解析下面就简单介绍一下JSO ...

  3. Android中解析Json数据

    在开发中常常会遇到解析json的问题 在这里总结几种解析的方式: 方式一: json数据: private String jsonData = "[{\"name\":\ ...

  4. iOS边练边学--iOS中的json数据解析

    JSON数据(NSData) -> OC对照表 {} -> NSDictionary @{} [] -> NSArray @[] "jack" -> NSS ...

  5. ios中解析json对象基类

    这个是对上面一篇写的一个解析json对象的基类 @interface BaseObjectFromJson : NSObject + (id) objectWithDict:(NSDictionary ...

  6. [转]在SqlServer 中解析JSON数据

      在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server ...

  7. C#中解析JSON数据,并获取到其中的值

    1.应需求创建一个Json字符串 string json = "[{'Name':'张三','age':'20','Data':{'ID':100,'heigh':'180','weight ...

  8. 在SqlServer 中解析JSON数据 [parseJSON] 函数 数据库中 解析JSON

    使用如下: SELECT * FROM parseJSON('{ "联系人": { "姓名": "huang", "网名" ...

  9. 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)

    在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...

随机推荐

  1. Linq101-Conversion Operators

    using System; using System.Linq; namespace Linq101 { class Conversion { /// <summary> /// This ...

  2. utils object doesn,t exists中毒后,就删除了.JS文件后台就出现了前面的英文。请问怎么解决

    明显是你删了js丢失了函数了...从备份里还原一份.js出来放进去,前台也不能删,如果遇到名字冲突,修改一下名字即可

  3. Swift 提示 error running playground...

    创建playground之后,我们将得到一个错误提示,Error running playground: Failed to prepare for communication with playgr ...

  4. groovy构建和解析xml文件

    原文链接:http://www.ibm.com/developerworks/cn/java/j-pg05199/ 代码示例: 构建xml文件: def static createXmlFile(){ ...

  5. 如何搭建一个angularJS应用

    以写一个hello word为例,首先引入angular.js库文件   <!doctype html> <html ng-app> <head> <titl ...

  6. jQuery获取JSON格式数据方法

    getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ ...

  7. 百度地图api窗口信息自定义

    百度地图加载完后,完全可以用dom方法操作,比较常用的就是点击mark的弹窗,利用jQuery可以很快的创建弹窗,需要注意的就是地图都是异步加载,所以绑定时间要用 jQuery 事件 - delega ...

  8. Altera quartus II遇到的问题

    编译时提示: Warning (13024): Output pins are stuck at VCC or GND Warning (13410): Pin "SCLK" is ...

  9. 改进基于Boost.Asio的聊天服务

    Boost.Asio是个非常易用的C++异步网络库,官方文档中一个示例是聊天服务,分为chat_message.chat_client.chat_server三个部分.chat_server的启动代码 ...

  10. poj 1066 Treasure Hunt

    http://poj.org/problem?id=1066 #include <cstdio> #include <cstring> #include <cmath&g ...