JSONKit解析

前端之家收集整理的这篇文章主要介绍了JSONKit解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#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

猜你在找的Json相关文章