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

JSONKit解析

发布时间:2020-12-16 19:03:55 所属栏目:百科 来源:网络整理
导读:#import "ViewController.h"#import "JSONKit.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. NSString *urlStri
#import "ViewController.h"
#import "JSONKit.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view,typically from a nib.
    
    NSString *urlString = @"https://api.douban.com/v2/book/search?q=s";
    
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {
        
        //json解析
        NSDictionary *dic = [data objectFromJSONData];
        
#if 0
        NSString *string ;
        
        //json解析
        [string objectFromJSONString];
#endif
        
        NSLog(@"%@",dic[@"total"]);
    }];
    
    /*
     username : "test"
     password : "xxx"
     other : {"key1":"value1","key2":["test","test2"]}
     */
    
    NSDictionary *dic = @{
        @"key1":@"value1",@"key2":@[@"test",@"test2"]
    };
    
    //提交到服务器的是json字符串
    //系统把对象(NSArray,NSDic)转化为json字符串
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    /*
     TestString:NSString
     
     TestString *s;
     */
    
    //判断string是否是NSString类或者其子类
    if ([string isKindOfClass:[NSString class]])
    {
        NSLog(@"YES");
    }
    
    //判断string是否是NSString类。不包括子类
    [string isMemberOfClass:[NSString class]];
    
    NSLog(@"--- %@",string);
    
    
#if 0
    /*
     在运行的时候和编译的时候data3是什么类型;
     编译是NSData;
     运行是NSString
     */
    NSData *data3 = [[NSString alloc] init];
    
    [data3 isEqualToData:[NSData data]];
#endif
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

(编辑:李大同)

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

    推荐文章
      热点阅读