sbjson框架使用 jsonDemo1 json操作 请求,下载,转码
来源:互联网 发布:linux shell comm 编辑:程序博客网 时间:2024/05/01 23:42
首先需要导入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解析数组操作
- sbjson框架使用 jsonDemo1 json操作 请求,下载,转码
- 不要使用SBJSON(json-framework)
- 不要使用SBJSON(json-framework)
- 不要使用SBJSON(json-framework)
- 不要使用SBJSON(json-framework)
- 不要使用SBJSON(json-framework)
- 不要使用SBJson(json-framework)
- 不要使用SBJSON(json-framework)
- 不要使用SBJSON(json-framework)
- 不要使用SBJSON(json-framework)
- SBJson 框架使用2 SBJson解析数据 解析字典,数组
- iOS 使用SBJSON创建和解析JSON
- iOS 使用SBJSON创建和解析JSON
- ASIHttpRequest+SBJson完成Http请求,以及json数据解析
- 使用SBJson
- IOS开发之——使用SBJson拼接Json字符串
- ios 中使用SBJson拼接和解析json
- 『IOS』不要使用SBJSON(json-framework)
- JSON入门教程
- 2013杭州区域赛现场赛 B && HDOJ 4771 —— 搜索
- 在Myeclipse9.0M1中安装SVN步骤
- 条款8:永不建立auto_ptr的容器
- 区间DP -- 石子合并(朴素DP)
- sbjson框架使用 jsonDemo1 json操作 请求,下载,转码
- 新人报道
- 【WebApi】————.net WebApi开发(一)
- 初识Python,画太阳黑子图
- ofstream ifstream 文件操作
- java提高篇(十二)-----equals()
- Java:多线程等待所有线程结束(CountDownLatch/CyclicBarrier)
- 数据库基础--视图
- php调用mysql中文时显示乱码的解决方法