码迷,mamicode.com
首页 > Web开发 > 详细

json

时间:2014-08-05 00:16:28      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   for   

 //json转换成oc对象
    NSString *jsonStr= @"{\"name\":\"Zhangsan\",\"age\":20,\"dog\":{\"name\":[\"XiaoHei\", \"XiaoQiang\"]}}";
    NSData *data=[jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);
    
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
    NSLog(@"%@",dic);
    NSLog(@"===%@",dic[@"age"]);
    NSLog(@"%@",dic[@"dog"][@"name"]);
    
    NSDictionary *dic2=[dic objectForKey:@"dog"];
    NSLog(@"%@",dic2);
    NSArray *array=[dic2 objectForKey:@"name"];
    NSLog(@"%@",[array objectAtIndex:1]);
   
    //oc对象装换成json
    NSDictionary *dic3=@{@"name":@[@"liSI",@"wangWu"],@"age":@32};
    NSData *data2=[NSJSONSerialization dataWithJSONObject:dic3 options:NSJSONWritingPrettyPrinted error:nil ];
    NSString *str2=[[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str2);
    


NSObject类中的
+ (BOOL)instancesRespondToSelector:(SEL)aSelector
用于判断某类中是否实现了某实例方法
URI 统一资源
URL 统一资源标识符


异步请求是不会等待的,同步是要等待的执行完才执行下一代码

、、、、、、、、、网络请求、、、、、、、
//
//  ViewController.m
//  NSObjc-0804
//
//  Created by apple on 14-8-4.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "ViewController.h"
#import "Ketter.h"
@interface ViewController ()
            

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
//    Ketter *k=[[Ketter alloc]init];
    NSURL *url=[NSURL URLWithString:@"http://www.baidu.com/"];
    NSLog(@"%@   %@",[url host],[url scheme]);
    NSURL *url2=[NSURL URLWithString:@"../../../" relativeToURL:url];
    NSLog(@"%@",url2);
    
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSLog(@"%@",[request HTTPMethod]);
    NSLog(@"%@",[request allHTTPHeaderFields]);
    
    //获取请求地址
    NSLog(@"%@",[request URL]);
    
    [request cachePolicy];
    
    NSData *data=[NSData dataWithContentsOfURL:url];
    NSLog(@"%d",data.length);
    
    //同步方法
    //调用一个方法的时候,如果该方法的返回依赖于它的功能是否完成,称为同步方法
//    NSData *data2=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%d",data.length);
    }];
    
    NSLog(@"======%@",request);
   // NSString
}

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

@end

json就是一个字符chuan

/////////////////////////////////////////////////////////////
 Kitty *k = [[Kitty alloc] init];
    [k print];
    
    //@selector(print) : myPrint
    
    //1. 运行时替换Kitty中print方法的实现
    class_replaceMethod([Kitty class], @selector(print), myPrint, NULL);
    //给k对象发送@selector(print)消息
//    [k print];
    
    class_addMethod([Kitty class], @selector(printx), myPrint, NULL);
    [k performSelector:@selector(printx)];
    
    //动态语言
    //swizzing
    
    //Objective-C方法的调用,发送消息
    objc_msgSend(k, @selector(print));
    
    //获取的是实例方法
    IMP call = [k methodForSelector:@selector(call)];
//    call(k, @selector(call));
    
    //子类的对象调用子类方法的实现
    SonKitty *son = [[SonKitty alloc] init];
    [son print];
    
    //本质上,用%@打印对象的时候,打印的是该对象调用description方法所返回的字符串
    NSLog(@"%@:%@", [son description], son);
    
    NSArray *array = @[@13, @"35", son];
    NSLog(@"%@", array);
    
    UIView *v = [[UIView alloc] init];
    NSLog(@"%@", v);
    
    //isKindOfClass匹配子类和父类的对象
    //isMemberOfClass严格匹配类型
    if (![son isMemberOfClass:[Kitty class]]) {
        NSLog(@"v is not kind of Kitty");
    }
    else {
        NSLog(@"v is kind of Kitty");
    }

 

json,布布扣,bubuko.com

json

标签:des   style   blog   http   color   os   io   for   

原文地址:http://www.cnblogs.com/lidongq/p/3891098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!