- 浏览: 534410 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tangyunliang:
大哥你太历害了谢谢
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
u013015029:
LZ,请问下,在// 添加消息到聊天窗口 , 这里获取Ed ...
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
endual:
怎么保持会话,我搞不懂啊
Android基于XMPP Smack Openfire开发IM【一】登录openfire服务器 -
donala_zq:
显示:[2013-11-30 11:50:36 - Andro ...
android-----------新浪微博 -
donala_zq:
哥,运行不了啊
android-----------新浪微博
先看下整个json数据的格式,如下:
工程结构图如下:
[img]
[/img]
1,先把SBJSON-Clssses这个文件夹导入工程里面
2,添加DataSource.h 与 DataSource.m到工程中
3、调用,在ViewController.h添加如下代码:
4、在在ViewController.m添加如下代码:
运行程序,打印结果如下:
{ "status": 200, "msg": "成功读取", "total": 4, "list": [ { "rid": "100001", "name": "倪氏海泰", "sofc": "testdata", "pcc": "88", "tastegrade": "1", "envgrade": "2", "servgrade": "3", "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs", "image": "http://www.dianxiaor.com/dicpics/100001/100001.jpg", "address": "海泰大厦", "lng": "116.372684", "lat": "39.993992", "distance": "0", "discounts": [ { "did": "1", "discount": "5", "stime": "01:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/a.jpg" }, { "did": "0", "discount": "5", "stime": "01:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/b.jpg" }, { "did": "2", "discount": "6", "stime": "05:00:00", "etime": "07:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/c.jpg" }, { "did": "0", "discount": "6", "stime": "05:00:00", "etime": "07:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/d.jpg" }, { "did": "3", "discount": "7", "stime": "08:00:00", "etime": "10:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/" }, { "did": "0", "discount": "7", "stime": "08:00:00", "etime": "10:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/" }, { "did": "4", "discount": "8", "stime": "21:00:00", "etime": "23:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/" }, { "did": "0", "discount": "8", "stime": "21:00:00", "etime": "23:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100001big/" } ] }, { "rid": "100002", "name": "大东北", "sofc": "testdata", "pcc": "88", "tastegrade": "1", "envgrade": "2", "servgrade": "3", "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs", "image": "http://www.dianxiaor.com/dicpics/100002/100002.jpg", "address": "志新东路", "lng": "116.375146", "lat": "39.994365", "distance": "213", "discounts": [ { "did": "1", "discount": "5", "stime": "01:00:00", "etime": "02:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/a.jpg" }, { "did": "0", "discount": "5", "stime": "01:00:00", "etime": "02:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/b.jpg" }, { "did": "2", "discount": "6", "stime": "03:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/c.jpg" }, { "did": "0", "discount": "6", "stime": "03:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/d.jpg" }, { "did": "3", "discount": "7", "stime": "08:00:00", "etime": "10:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/" }, { "did": "0", "discount": "7", "stime": "08:00:00", "etime": "10:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/" }, { "did": "4", "discount": "8", "stime": "21:00:00", "etime": "23:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/" }, { "did": "0", "discount": "8", "stime": "21:00:00", "etime": "23:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100002big/" } ] }, { "rid": "100003", "name": "伊喜饺子", "sofc": "testdata", "pcc": "66", "tastegrade": "1", "envgrade": "2", "servgrade": "3", "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs", "image": "http://www.dianxiaor.com/dicpics/100003/100003.jpg", "address": "志新西路", "lng": "116.371552", "lat": "39.99612", "distance": "255", "discounts": [ { "did": "1", "discount": "5", "stime": "01:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/a.jpg" }, { "did": "0", "discount": "5", "stime": "01:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/b.jpg" }, { "did": "2", "discount": "6", "stime": "05:00:00", "etime": "06:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/c.jpg" }, { "did": "0", "discount": "6", "stime": "05:00:00", "etime": "06:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/d.jpg" }, { "did": "3", "discount": "7", "stime": "08:00:00", "etime": "09:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/" }, { "did": "0", "discount": "7", "stime": "08:00:00", "etime": "09:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/" }, { "did": "4", "discount": "8", "stime": "21:00:00", "etime": "23:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/" }, { "did": "0", "discount": "8", "stime": "21:00:00", "etime": "23:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100003big/" } ] }, { "rid": "100004", "name": "小街卤煮", "sofc": "testdata", "pcc": "66", "tastegrade": "1", "envgrade": "2", "servgrade": "3", "recomm": "menu001 adfads dsfgsfe sdsfddasf sdfsf sdfsdf sdsfcsd efrfs", "image": "http://www.dianxiaor.com/dicpics/100004/100004.jpg", "address": "志新西路", "lng": "116.37272", "lat": "39.997516", "distance": "391", "discounts": [ { "did": "1", "discount": "5", "stime": "01:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100004big/a.jpg" }, { "did": "0", "discount": "5", "stime": "01:00:00", "etime": "04:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100004big/b.jpg" }, { "did": "2", "discount": "6", "stime": "06:00:00", "etime": "07:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100004big/c.jpg" }, { "did": "0", "discount": "6", "stime": "06:00:00", "etime": "07:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100004big/d.jpg" }, { "did": "3", "discount": "7", "stime": "09:00:00", "etime": "10:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100004big/" }, { "did": "0", "discount": "7", "stime": "09:00:00", "etime": "10:00:00", "available": 10, "dimg": "http://www.dianxiaor.com/dicpics/100004big/" }, { "did": "4", "discount": "8", "stime": "00:00:00", "etime": "00:00:00", "available": 0, "dimg": "http://www.dianxiaor.com/dicpics/100004big/" } ] } ] }
工程结构图如下:
[img]
[/img]
1,先把SBJSON-Clssses这个文件夹导入工程里面
2,添加DataSource.h 与 DataSource.m到工程中
@interface DataSource : NSObject //becarefull NSString is a object +(id)dataSource:(NSString *)str; +(id)dataSourceFromUrl:(NSString *)url; @end
#import "JSON.h" #import "DataSource.h" @implementation DataSource //解析本地json数据 +(id)dataSource:(NSString *)str { //获取文件路径 NSString * path = [[NSBundle mainBundle] pathForResource:str ofType:@""]; //获取数据 NSData * reader = [NSData dataWithContentsOfFile:path]; //转换成NSString NSString * JSONString = [[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding]; return [JSONString JSONValue]; } //解析网络json数据,传入url +(id)dataSourceFromUrl:(NSString *)url{ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; //获取数据 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //转换成NSString NSString * JSONString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; return [JSONString JSONValue]; } @end
3、调用,在ViewController.h添加如下代码:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { NSDictionary * myData; } @property (retain, nonatomic) NSDictionary * myData; @end
4、在在ViewController.m添加如下代码:
// // ViewController.m // JsonTestCao // // Created by 张 志亮 on 13-1-3. // Copyright (c) 2013年 张 志亮. All rights reserved. // #import "ViewController.h" #import "DataSource.h" @interface ViewController () @end @implementation ViewController @synthesize myData; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //这是模拟操作json解析,用的是本地的json数据 //获取数据源 // NSString *str = @"jsondata"; // myData = [DataSource dataSource:str]; // NSInteger numbers = [[myData valueForKey:[[myData allKeys] objectAtIndex:0]] count]; // NSString *sectionNanme = [[myData allKeys]objectAtIndex:1]; // NSString *name = [[myData valueForKey:sectionNanme]objectAtIndex:0]; // NSLog(@"%d",[myData count]); // NSLog(@"%d",numbers); // NSLog(@"%@",sectionNanme); // NSLog(@"%@",name); //获得url NSString *url = @"http://www.dianxiaor.com/getrinfos.php?lng=116.372684&lat=39.993995#jsonNav_l"; //获得数据 myData = [DataSource dataSourceFromUrl:url]; NSLog(@"%d",[myData count]); //获得status NSString *status = [myData valueForKey:@"status"]; NSLog(@"%@",status); //获得msg NSString *msg = [myData valueForKey:@"msg"]; NSLog(@"%@",msg); //获得list的长度 NSInteger countList = [[myData valueForKey:@"list"]count]; NSLog(@"%d",countList); //获得rid NSString *rid = [[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"rid"]; NSLog(@"%@",rid); //获得address NSString *address = [[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"address"]; NSLog(@"%@",address); //获得discounts count 8 NSInteger discounts = [[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]count]; NSLog(@"discounts==%d",discounts); //获得list第一项里面discounts里面所有的dimg NSString *dimg = [[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]valueForKey:@"dimg"]; //this is all dimg for "objectAtIndex:0" and "discounts" NSLog(@"dimg==%@",dimg); //获得list第一项里面discounts里面,一共有8项 第一项的dimg的值 NSString *firstDimg = [[[[[myData valueForKey:@"list"]objectAtIndex:0]valueForKey:@"discounts"]objectAtIndex:0]valueForKey:@"dimg"]; NSLog(@"firstDimg=%@",firstDimg); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
运行程序,打印结果如下:
2013-01-04 10:42:26.691 JsonTestCao[476:c07] 4 2013-01-04 10:42:26.695 JsonTestCao[476:c07] 200 2013-01-04 10:42:26.695 JsonTestCao[476:c07] 成功读取 2013-01-04 10:42:26.695 JsonTestCao[476:c07] 4 2013-01-04 10:42:26.695 JsonTestCao[476:c07] 100001 2013-01-04 10:42:26.696 JsonTestCao[476:c07] 海泰大厦 2013-01-04 10:42:26.696 JsonTestCao[476:c07] discounts==8 2013-01-04 10:42:26.697 JsonTestCao[476:c07] dimg==( "http://www.dianxiaor.com/dicpics/100001big/a.jpg", "http://www.dianxiaor.com/dicpics/100001big/b.jpg", "http://www.dianxiaor.com/dicpics/100001big/c.jpg", "http://www.dianxiaor.com/dicpics/100001big/d.jpg", "http://www.dianxiaor.com/dicpics/100001big/", "http://www.dianxiaor.com/dicpics/100001big/", "http://www.dianxiaor.com/dicpics/100001big/", "http://www.dianxiaor.com/dicpics/100001big/" ) 2013-01-04 10:42:26.697 JsonTestCao[476:c07] firstDimg=http://www.dianxiaor.com/dicpics/100001big/a.jpg
发表评论
-
新风作浪博客学习(十九)在iOS虚拟键盘上添加动态隐藏按钮
2013-06-08 09:19 859为了给用户比较良好的交付,想在键盘上添加一个按钮,实时根据键盘 ... -
新风作浪博客学习(十八)openURL的使用(iOS调用系统电话、浏览器、地图、邮件等) .
2013-06-08 09:19 1003今天遇见一行代码实现打开一个网页,比起印象里的UIWebVie ... -
新风作浪博客学习(十七)UIImageView响应点击事件 .
2013-06-08 09:19 703有时候会遇到点击一张图片,然后让这张图片触发一个事件,或者是跳 ... -
新风作浪博客学习(十六)Navigation + Tab Bar 常用组合框架 .
2013-06-07 08:50 1248看到很多项目中都采用的是Navigation加Tab Bar组 ... -
新风作浪博客学习(十五)google地图定位小Demo .
2013-06-07 08:50 1138[img][/img]今天写的是一个简单功能的google地图 ... -
新风作浪博客学习(十四)怎样向iPhone模拟器中添加图片 .
2013-06-07 08:50 785在我们做项目中可能需要使用图库,模拟器是有图库的,但是如何像其 ... -
新风作浪博客学习(十三)表视图的分组分区和索引分区 .
2013-06-07 08:50 796本次实现的是表视图的分区和索引,代码和前面都差不多,主要还是代 ... -
新风作浪博客学习(十二)代码实现UITableViewCell表视图单元定制 .
2013-06-07 08:49 997通常情况下我们会希望单元格UITableViewCell显示自 ... -
新风作浪博客学习(十一)UITableViewCell的标记、移动、删除、插入 .
2013-06-06 09:15 1115这篇文章是建立在 代码实现 UITableView与UITa ... -
新风作浪博客学习(十)代码实现 UITableView与UITableViewCell .
2013-06-06 09:14 1153我们常用的表格类视图就是用 UITableView与UITab ... -
新风作浪博客学习(九)两个UIPickerView控件间的数据依赖 .
2013-06-06 09:14 1071本篇实现功能是两个选取器的关联操作,滚动第一个滚轮第二个滚 ... -
新风作浪博客学习(八)代码实现UIPickerView .
2013-06-06 09:14 1282先说一下当个组件选取器,我们创建一个数组NSAray来保存选取 ... -
新风作浪博客学习(七)代码 实现UIDatePicker控件 和 Tab Bar 视图切换 .
2013-06-06 09:15 1107感觉代码写控件都一个理,先在ViewDidLoad中创建控件对 ... -
新风作浪博客学习(六)ios 视图切换翻页效果 .
2013-06-05 11:18 1060本文写的是视图切换,涉及到的内容有 1.实现代码添加Navi ... -
新风作浪博客学习(五)代码实现UISlider 和 UISwitch .
2013-02-18 09:15 1149本次实现的UISlider和UISwi ... -
新风作浪博客学习(四)把plist里数据显示在textField上 .
2013-02-18 09:15 916在代码实现Lable 、textFie ... -
新风作浪博客学习(三)NSBundle读取图片 plist文件和txt文件
2013-02-18 09:15 1728本文想简单介绍一下NSBundle读取图片到视图上,读取pli ... -
新风作浪博客学习(二)代码实现Lable 、textField创建界面以及键盘的处理
2013-02-18 09:15 1173今天写的是用代码实现一个简单界面,代码重复率比较高,可读性不是 ... -
新风作浪博客学习(一)plist文件读写操作
2013-02-18 09:14 1361文件plist 全名Property List,属性列表文件, ... -
GCDiscreetNotificationView提示视图
2013-06-05 11:17 559先看一下效果图: [img] ...
相关推荐
本教程将深入讲解iPhone开发中的JSON解析,帮助开发者更好地理解和运用这一技术。 首先,我们需要了解JSON的基本结构。JSON是一种轻量级的数据格式,其数据结构主要由对象(对象是一个键值对的集合,用花括号 `{}` ...
本项目标题“iPhone开发-qq聊天代码”表明我们将深入探讨如何在iPhone应用中集成并实现QQ聊天功能,包括支持表情输入。下面将详细介绍这个过程中涉及的关键知识点。 1. **Swift或Objective-C编程**:iOS应用开发...
在iPhone开发中,苹果的Foundation框架提供了一个名为`NSJSONSerialization`的类,它是处理JSON的主要工具。以下是如何使用`NSJSONSerialization`解析JSON数据的步骤: 1. **获取JSON数据**:通常,JSON数据来自于...
在Android开发中,处理JSON数据是一项基础且重要的任务。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。本篇文章主要探讨了如何在Android环境中解析...
本资料“Json.zip”专注于讲解如何在iPhone应用中使用JSON,特别是涉及到将JSON解析为字典以及将字典转换回JSON格式的逆向操作。 首先,让我们深入理解JSON的格式。JSON是一种基于文本的格式,其数据结构主要由对象...
总的来说,这个项目涵盖了iOS开发中的网络请求、JSON解析以及模型对象的创建和使用。理解这些概念和技术对于任何iOS开发者来说都是非常基础且重要的,尤其是在构建需要与服务器交互的应用时。通过实践和学习这些知识...
iphone开发之Json数据的解析 1、将Source文件加入工程,在合适的位置添加CJSONDeserializer.h头文件。 2、从服务器请求数据、解析
最后,教程可能会涉及网络编程,包括使用URLSession进行HTTP请求,以及JSON解析,这些都是现代应用与服务器交互的基础。 总的来说,《iPhone开发基础教程-PDF版》提供了一个全面的学习路径,从入门到进阶,帮助...
- **JSON/XML解析**: 处理服务器返回的数据格式,实现数据与应用逻辑的交互。 - **WebSocket**: 实时双向通信技术,适用于聊天应用或实时更新场景。 #### 5. 测试与调试 - **XCTest**: 苹果提供的单元测试框架,...
3. JSON解析:使用`JSONSerialization`或第三方库如SwiftyJSON处理JSON数据。 4. UI自动化测试:可能使用XCTest进行应用的功能测试。 5. 数据持久化:可能采用Core Data或SQLite存储用户数据。 6. 自定义控件:...
《Head First iPhone开发》这本书由Dan Pilone撰写,是一本专为希望快速掌握iPhone应用程序开发技术的编程者设计的教程。该书通过简单、步骤化的教学方式,将复杂的概念和技巧以易于理解的形式呈现给读者,使他们...
8. **网络编程**:包括使用URLSession进行HTTP请求,JSON解析,以及使用Core Data存储本地数据。 9. **推送通知**:实现远程通知,让用户即使不打开应用也能获取信息。 10. **App Store发布**:包括证书配置、版本...
- **网络通信**:详解了如何利用NSURLSession进行网络请求,以及如何解析JSON、XML等数据格式。 #### 4. 实战案例 - **完整项目开发**:通过一个完整的项目实例,展示了从需求分析到设计、编码、测试直至发布的全...
同时,优化JSON解析和序列化性能对于提升用户体验至关重要,尤其是在处理大量数据时。 9. **JSON和Model对象映射**:在实际项目中,通常会创建特定的Model类来代表JSON数据结构,通过工具或手动方式实现JSON数据到...
这个压缩包“fastjson解析json.rar”显然包含了使用Fastjson进行JSON解析的相关资源,包括fastjson的库文件、示例代码以及可能的数据模型类。下面将详细介绍Fastjson的使用方法以及压缩包中的关键文件。 首先,`...
通过对源代码的详细解析,我们将揭示iOS应用开发的核心概念和技术,包括Objective-C编程语言、UIKit框架、游戏逻辑设计以及资源管理等多个方面。 首先,我们关注的是Objective-C,这是iOS应用开发的主要语言。...
3. **网络请求处理**:了解如何使用URLSession或其他第三方库如Alamofire进行API调用,获取和解析JSON数据。 4. **UI设计与实现**:学习如何使用Storyboard、XIB或SwiftUI创建用户界面,以及AutoLayout和Size ...
书中会介绍如何处理JSON数据,以及如何使用JSONSerialization进行数据解析。 在用户体验方面,iOS强调原生设计原则,遵循苹果的设计指南,提供一致且直观的交互。开发者需要学习如何利用Auto Layout和Size Classes...
此外,JSON是最常见的数据交换格式,书中会解释如何解析和序列化JSON数据。 iOS应用还需要处理用户的输入和交互,如触摸事件、手势识别等。此外,通知、定位服务、多媒体播放等也是iOS开发中的重要话题。本书会详细...