利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
来源:互联网 发布:ubuntu升级16.04后黑屏 编辑:程序博客网 时间:2024/06/01 15:09
整ASIHTTPRequest和JSONKit花了我近三个晚上(20点到23点30)的时间,今天终于把后台返回的json串解析出来了,为了模拟真实的调用环境,用spring mvc搭了个简单的java后台服务。提供json数据
运行如下
同步调用、异步调用、块格式调用 说的是用ASIHTTPRequest请求后台的方式。下面有一个按钮是批JSON解析
其实ASIHTTPRequest请求后台都没什么好说的,官方的API说的很清楚,可以看看官方的how to use
时间主要都花在解析后台返回的数据上了。
下面上代码
controller.h文件复制代码ASIHTTPRequest的使用就不写了,官方都有
下面看看调用后台的吧 ,
先贴一下,java端返回的数据:
这个方法是JSONKit远程测试的复制代码先用NSUrl构造一个url,然后用ASIHTTPRequest请求,如果没错误的情况下用NSString来接收返回的值复制代码调用JSONKit转成NSArray复制代码时间就是花在这了,当时不知道这个地方到底该用什么接收,网上查都说是用NSDictionary,但是用这个就一直报错,后面听群里的网友说,可以用[obj class]来确定是什么类型,试了下,果真好使,,,,到这基本上就没什么问题了 ,哈哈。
当用[obj class]得知返回的数据是Array之后,就直接用NSArray接收就好了,后面什么都通顺了。。。
点击JSONKit远程测试,控制台输出:
记录一下,当时纠结的心情只有自己知道。
路漫漫其修远兮,吾将上下而求索。
运行如下
同步调用、异步调用、块格式调用 说的是用ASIHTTPRequest请求后台的方式。下面有一个按钮是批JSON解析
其实ASIHTTPRequest请求后台都没什么好说的,官方的API说的很清楚,可以看看官方的how to use
时间主要都花在解析后台返回的数据上了。
下面上代码
controller.h文件
- #import <UIKit/UIKit.h>
- #import "ASIHTTPRequest.h"
- #import "JSONKit.h"
- @interface ITViewController : UIViewController
- @property (strong, nonatomic) IBOutlet UILabel *label;
- //同步调用
- - (IBAction)buttonPressed:(id)sender;
- //异步调用
- - (IBAction)asyButtonPressed:(id)sender;
- //块格式调用
- - (IBAction)blockButtonPressed:(id)sender;
- //JSONKit测试
- - (IBAction)jsonKitTestButtonPressed:(id)sender;
- //JSONKit本地测试
- -(IBAction)localJsonKitTest:(id)sender;
- //JSONKit返回集合数据(集合中都是原始字段的数据)
- - (IBAction)jsonKitArrayListWithYY:(id)sender;
- @end
下面看看调用后台的吧 ,
先贴一下,java端返回的数据:
[
{
"name": "北京",
"id": 1,
"routes": [
{
"id": 1,
"content": "北京一日游,好玩",
"days": 1,
"title": "北京一日游",
"startDate": 1348070400000,
"endDate": 1345392000000,
"scenerySpot": "天安门、长城",
"price": 168,
"districtId": 1,
"bigImgId": 1,
"samllImgId": 1,
"specialPrice": 1,
"routeDate": null,
"clickCount": null,
"isRecommend": null
},
{
"id": 2,
"content": "北京长城 十三陵一日游",
"days": 1,
"title": "北京长城 十三陵一日游",
"startDate": 1345478400000,
"endDate": 1345478400000,
"scenerySpot": "长城 十三陵",
"price": 190,
"districtId": 1,
"bigImgId": 1,
"samllImgId": 1,
"specialPrice": 1,
"routeDate": null,
"clickCount": null,
"isRecommend": null
}
]
},
{
"name": "上海",
"id": 2,
"routes": []
},
{
"name": "湖南",
"id": 3,
"routes": []
},
{
"name": "湖北",
"id": 4,
"routes": []
},
{
"name": "浙江",
"id": 5,
"routes": []
},
{
"name": "江苏",
"id": 6,
"routes": []
},
{
"name": "福建",
"id": 7,
"routes": []
},
{
"name": "新疆",
"id": 8,
"routes": []
}
]
这个方法是JSONKit远程测试的
- //JSONKit测试
- - (IBAction)jsonKitTestButtonPressed:(id)sender{
-
- //本地测试json转对象
- // NSString *strJson = @"{\"aps\": {\"alert\":{\"body\":\"a msg come!\"},\"bage\":3,\"sound\":\"def.mp3\"}}";
- // NSDictionary *result = [strJson objectFromJSONString];
- // NSLog(@"%@",result);
-
- //本地测试对象转json
- //生成json数据
- // NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
- // NSMutableDictionary *alert = [NSMutableDictionary dictionary];
- // NSMutableDictionary *aps = [NSMutableDictionary dictionary];
- // [alert setObject:@"a msg come!" forKey:@"body"];
- // [aps setObject:alert forKey:@"alert"];
- // [aps setObject:@"3" forKey:@"bage" ];
- // [aps setObject:@"def.mp3" forKey:@"sound"];
- // [jsonDic setObject:aps forKey:@"aps"];
- // NSString *strJson = [jsonDic JSONString];
- // NSLog(@"%@",strJson);
-
- //远程测试读取java服务中返回的json
- //构造一个url http://192.168.0.92:8080/ly/w/route/ios_test
- //http://192.168.0.92:8080/ly/w/route/findRouteByCategoryId?categoryId=1
- NSURL *url = [NSURL URLWithString:@"http://192.168.0.92:8080/ly/w/route/findRouteByCategoryId?categoryId=1"];
-
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request startSynchronous];
-
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
-
- NSLog(@"请求的结果是;%@",response);
- label.text = @"succeeded";
-
- //用[obj class]查看后发现返回的数据是JKArray
- NSArray *result = [response objectFromJSONString];
- NSLog(@"%@",[result class]);
-
- NSLog(@"%@",[[result objectAtIndex:0] class]);
-
- for (int i = 0; i < [result count]; i++) {
- //取得result的每一条记录
- NSDictionary *di1 = [result objectAtIndex:i];
- NSString *t_id = [di1 objectForKey:@"id"];
- NSString *t_name = [di1 objectForKey:@"name"];
- NSLog(@"第%d条记录的id值是%@",i+1,t_id);
- NSLog(@"第%d条记录的name值是%@",i+1,t_name);
-
- //取得每个城市下的线路routes
- NSArray *routes = [di1 objectForKey:@"routes"];
- //循环线路
- for (NSDictionary *route in routes) {
- //取得每个线路下的标题
- NSString *title = [route objectForKey:@"title"];
- NSLog(@"%@",title);
- }
-
- }
-
-
- }
- }
- NSString *response = [request responseString];
- [response objectFromJSONString];
当用[obj class]得知返回的数据是Array之后,就直接用NSArray接收就好了,后面什么都通顺了。。。
点击JSONKit远程测试,控制台输出:
记录一下,当时纠结的心情只有自己知道。
路漫漫其修远兮,吾将上下而求索。
- 利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
- 利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
- JSON 数据的 JSONKit 解析
- JSon解析之JSonKit
- Gson利用泛型将Http请求返回的Json字符串快速解析模板
- JSON解析详解(JSONKit、苹果自带解析)和PList文件的反序列化
- IOS中Json解析的四种方法,推荐使用原生解析,和jsonKit
- JSON解析-系统方法和第三方JSONKit的简单实用
- iOS中json解析你还停留在JSONKit吗?NSJSONSerialization和JSONKit性能比较
- iOS之JSONKit解析JSON
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 选取函数库ASIHttpRequest和JsonKit原因
- Http请求返回json解析java.lang.IndexOutOfBoundException错误
- JAVA 请求http接口返回json数据解析
- JSONKit 解析逗号冗余的json字符串失败
- ASIHttpRequest+SBJson完成Http请求,以及json数据解析
- MyEclipse中文乱码问题
- 对象析构谈—— delete this 的使用及注意事项
- Spring中@Autowired注解、@Resource注解的区别
- zabbix 自定义监控
- Java mock工具-mockito
- 利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- Ubuntu 12.04下编译OpenSceneGraph-3.0.1记录
- 程序员之路——一个老程序员对刚上大学的学弟学妹的忠告
- 使用“数据库配置助手”时报“使用Database Control配置数据库时,……配置监听程序”的警告
- 堆和栈的区别
- C#索引器
- 第一篇博客 写于2013春实训后
- C专家编程 前言 第一章