Json
发布时间:2019-10-11 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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$"}]
*/
NS
String* str = @"
{\"book\":\"甄嬛传\",\"jianjie\":\"大清后宫\"}";
//通过JSONValue方法解析json字符串得到字典或数组对象
NS
Dictionary*
Dic = [str JSONValue];
NSLog(@"%@",[
Dic ob
jectForKey:@"book"]
);
str = @"[\"甄嬛传\",\"超人\",\"蜘蛛侠\"]";
NSArray* array = [str JSONValue];
for (NS
String* bookStr in array)
{
NSLog(@"%@",bookStr
);
}
/*
http://www
.baidu.com/abc/bbc/1.jpg
http:// 请求类型
http:// ftp://
www
.baidu.com 请求地址 ip
/abc/bbc/1.jpg 路径
*/
//请求地址
NS
String* urlStr = @"
http://192.168.88.8/sns/my/
user_list.
php";
NSURL* url = [NSURL URLWith
String:urlStr];
str = [NS
String
StringWithContentsOfURL:url encoding:NSUTF8
StringEncoding error:nil];
Dic = [str JSONValue];
array = [
Dic ob
jectForKey:@"users"];
NS
Dictionary* user
Dic = [array ob
jectA
Tindex:0];
NS
String* imageUrl = [user
Dic ob
jectForKey:@"headimage"];
imageUrl = [NS
String
StringWithFormat:@"
http://192.168.88.8/sns%@",imageUrl];
NSLog(@"%@",imageUrl
);
//下载数据
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWith
String:imageUrl]];
//NSData->UIImage
UIImage* image = [UIImage imageWithData:data];
//UIImage->UIColor
self.view.
BACkgroundColor = [UIColor colorWithPatternImage:image];
//NSData->NS
String
//NS
String* str = [[NS
String alloc] initWithData:data encoding:NSUTF8
StringEncoding];
str = @"
http://book.douban.com/sub
ject_search?search_text=甄嬛传&cat=1001";
url = [NSURL URLWith
String:str];
str = [NS
String
StringWithContentsOfURL:url encoding:NSUTF8
StringEncoding error:nil];
NSLog(@"%@",str
);
//中文转码
str = @"甄嬛传";
str = [str
StringByAddingPercentEscapesUsingEncoding:NSUTF8
StringEncoding];
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
{
NS
String* str = @"
http://pic.desk.chinaz.com/file/201207/7/kamchongerczwp1.jpg";
NSURL* url = [NSURL URLWith
String:str];
NSData* data = [NSData dataWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:data];
self.view.
BACkgroundColor = [UIColor colorWithPatternImage:image];
}
@end
后面还有json解析数组操作
大佬总结
以上是大佬教程为你收集整理的sbjson框架使用 jsonDemo1 json操作 请求,下载,转码全部内容,希望文章能够帮你解决sbjson框架使用 jsonDemo1 json操作 请求,下载,转码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。