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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值