加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:20:18 所属栏目:百科 来源:网络整理
导读:首先需要导入SBJson框架 在导入头文件 在进行操作 #import "SBJson.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //XML JSON 描述性语言 //甄嬛传 大清后宫。。。 99$ //{"book":"甄嬛传","jianjie":"大清后宫","price":"

首先需要导入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解析数组操作

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读