sbjson框架使用 jsonDemo1 json操作 请求,下载,转码

前端之家收集整理的这篇文章主要介绍了sbjson框架使用 jsonDemo1 json操作 请求,下载,转码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先需要导入SBJson框架 在导入头文件 在进行操作

#import "SBJson.h"


@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

//XML JSON 描述性语言

//甄嬛传 大清后宫。。。 99$
//{"book":"甄嬛传","jianjie":"大清后宫","price":"99$"}
//["甄嬛传","超人","蜘蛛侠"]
/*
[{"book":"甄嬛传","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,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解析数组操作

猜你在找的Json相关文章