运行如下
同步调用、异步调用、块格式调用 说的是用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": "北京长城 十三陵一日游",
"title": "北京长城 十三陵一日游",
"startDate": 1345478400000,
"endDate": 1345478400000,
"scenerySpot": "长城 十三陵",
"price": 190,
"isRecommend": null
}
]
},
{
"name": "上海",
"id": 2,
"routes": []
},
{
"name": "湖南",
"id": 3,
{
"name": "湖北",
"id": 4,
{
"name": "浙江",
"id": 5,
{
"name": "江苏",
"id": 6,
{
"name": "福建",
"id": 7,
{
"name": "新疆",
"id": 8,
"routes": []
}
]
这个方法是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];
- //远程测试读取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]);
- 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值是%@",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远程测试,控制台输出:
2012-9-13 23:32:28 上传
下载附件(97.16 KB)
路漫漫其修远兮,吾将上下而求索。 原文链接:https://www.f2er.com/json/290484.html