sbjson框架使用 jsonDemo1 json操作 请求,下载,转码
2013-11-09 22:45
369 查看
首先需要导入SBJson框架 在导入头文件 在进行操作
#import "SBJson.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//XML JSON 描述性语言
//甄嬛传 大清后宫。。。 99$
//{"book":"甄嬛传","jianjie":"大清后宫","price":"99$"}
//["甄嬛传","超人","蜘蛛侠"]
/*
[{"book":"甄嬛传","jianjie":"大清后宫","price":"99$"},{"book":"超人","jianjie":"美国后宫","price":"9$"}]
*/
NSString* str = @"{\"book\":\"甄嬛传\",\"jianjie\":\"大清后宫\"}";
//通过JSONValue方法解析json字符串得到字典或数组对象
NSDictionary* dic = [str JSONValue];
NSLog(@"%@",[dic objectForKey:@"book"]);
str = @"[\"甄嬛传\",\"超人\",\"蜘蛛侠\"]";
NSArray* array = [str JSONValue];
for (NSString* bookStr in array) {
NSLog(@"%@",bookStr);
}
/*
http://www.baidu.com/abc/bbc/1.jpg http:// 请求类型 http:// ftp://
www.baidu.com 请求地址 ip
/abc/bbc/1.jpg 路径
*/
//请求地址
NSString* urlStr = @"http://192.168.88.8/sns/my/user_list.php";
NSURL* url = [NSURL URLWithString:urlStr];
str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
dic = [str JSONValue];
array = [dic objectForKey:@"users"];
NSDictionary* userDic = [array objectAtIndex:0];
NSString* imageUrl = [userDic objectForKey:@"headimage"];
imageUrl = [NSString stringWithFormat:@"http://192.168.88.8/sns%@",imageUrl];
NSLog(@"%@", imageUrl);
//下载数据
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
//NSData->UIImage
UIImage* image = [UIImage imageWithData:data];
//UIImage->UIColor
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
//NSData->NSString
//NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
str = @"http://book.douban.com/subject_search?search_text=甄嬛传&cat=1001";
url = [NSURL URLWithString:str];
str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str);
//中文转码
str = @"甄嬛传";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 40);
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClick{
NSString* str = @"http://pic.desk.chinaz.com/file/201207/7/kamchongerczwp1.jpg";
NSURL* url = [NSURL URLWithString:str];
NSData* data = [NSData dataWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:data];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
@end
后面还有json解析数组操作
#import "SBJson.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//XML JSON 描述性语言
//甄嬛传 大清后宫。。。 99$
//{"book":"甄嬛传","jianjie":"大清后宫","price":"99$"}
//["甄嬛传","超人","蜘蛛侠"]
/*
[{"book":"甄嬛传","jianjie":"大清后宫","price":"99$"},{"book":"超人","jianjie":"美国后宫","price":"9$"}]
*/
NSString* str = @"{\"book\":\"甄嬛传\",\"jianjie\":\"大清后宫\"}";
//通过JSONValue方法解析json字符串得到字典或数组对象
NSDictionary* dic = [str JSONValue];
NSLog(@"%@",[dic objectForKey:@"book"]);
str = @"[\"甄嬛传\",\"超人\",\"蜘蛛侠\"]";
NSArray* array = [str JSONValue];
for (NSString* bookStr in array) {
NSLog(@"%@",bookStr);
}
/*
http://www.baidu.com/abc/bbc/1.jpg http:// 请求类型 http:// ftp://
www.baidu.com 请求地址 ip
/abc/bbc/1.jpg 路径
*/
//请求地址
NSString* urlStr = @"http://192.168.88.8/sns/my/user_list.php";
NSURL* url = [NSURL URLWithString:urlStr];
str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
dic = [str JSONValue];
array = [dic objectForKey:@"users"];
NSDictionary* userDic = [array objectAtIndex:0];
NSString* imageUrl = [userDic objectForKey:@"headimage"];
imageUrl = [NSString stringWithFormat:@"http://192.168.88.8/sns%@",imageUrl];
NSLog(@"%@", imageUrl);
//下载数据
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
//NSData->UIImage
UIImage* image = [UIImage imageWithData:data];
//UIImage->UIColor
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
//NSData->NSString
//NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
str = @"http://book.douban.com/subject_search?search_text=甄嬛传&cat=1001";
url = [NSURL URLWithString:str];
str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str);
//中文转码
str = @"甄嬛传";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 40);
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClick{
NSString* str = @"http://pic.desk.chinaz.com/file/201207/7/kamchongerczwp1.jpg";
NSURL* url = [NSURL URLWithString:str];
NSData* data = [NSData dataWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:data];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
@end
后面还有json解析数组操作
相关文章推荐
- Android网络框架-Volley(四) 使用get和post方法发送json请求
- [原创]jPagerBar1.2发布(一个适合Ajax+JSON+jQuery环境使用的多功能页码栏插件)(Demo、源文件下载已更新)
- 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
- IOS中json字符串原生数据请求&IOS跨平台AES128字符串加解密&AFNetworking框架的简单封装使用
- mongodb 在yii2 框架中的使用(本人在框架中做了功能扩展,非非官方下载的yii2的操作)
- 使用google的开源框架Volley,功能一:网络请求Json数据
- Android网络请求操作Volley框架的使用
- 网络请求----简单框架使用------(百度图片下载实例)
- 使用开源框架AsyncHttpClient进行Json的Post请求
- [原创]jPagerBar1.2发布-jquery分页插件(一个适合Ajax+JSON+jQuery环境使用的多功能页码栏插件)(Demo、源文件下载已更新)
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
- HTML之AJAX、GET、POST、GETJSON请求使用和同步操作-yellowcong
- Android网络框架-Volley(四) 使用get和post方法发送json请求
- xutil3网络请求参数使用json结构
- ios 中使用SBJson拼接和解析json
- dubbo框架讲解以及dubbo源码demo下载
- 第十七天3月12日之一句代码实现crud操作(手动写的,没使用其他jar包和框架)
- Android BLE基础操作框架使用详解
- 使用Hibernate框架操作Mysql数据库时,插入中文字符乱码!已解决!