JSON&XML

JSON(NSData) --> OC对象

  • { } --> NSDictionary@{}
  • [ ] --> NSArray@[]
  • ""--> NSString@""
  • 10--> NSNumber @10
  • true --> NSNumber @1

JSON(NSData) --> OC对象


NSJSONReadingMutableContainers = (1UL << 0),//创建出来的数组和字典是可变的
NSJSONReadingMutableLeaves = (1UL << 1),//数组和字典里里面的字符串是可变的
NSJSONReadingAllowFragments = (1UL << 2)//允许解析出来的对象不是字典或者数组

NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

OC对象 --> SON(NSData)

NSData * data1 = [NSJSONSerialization dataWithJSONObject:@{@"name":@"jake"} options:NSJSONWritingPrettyPrinted error:nil];

利用苹果自带的API播放视频

NSDictionary * dict = self.videos[indexPath.row];
    
    NSString * url = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:dict[@"url"]];
    
    //创建视频播放器
    
    MPMoviePlayerViewController * movie = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:url]];
    
//    [self presentViewController:movie animated:YES completion:nil];
    [self presentMoviePlayerViewControllerAnimated:movie];

XML的解析方式

  • SAX
    • 大小文件都可以
    • NSXMLParser
  • DOM
    • 最后是小文件
    • GdataXML

NSXMLParser的用法

  • 创建解析器开始解析
//解析XML数据
//1.创建XML解析器
NSXMLParser * parser = [[NSXMLParser alloc]initWithData:data];

//设置代理
parser.delegate = self;

//2.开始解析
[parser parse];
  • 代理遵守协议 实现代理方法
//解析到某个元素
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"videos"]) return;
    
    //第一种方案
//    Video * v = [[Video alloc]init];
//    
//    v.keyValues = attributeDict;
    
    //第二种方案
    Video * v = [Video objectWithKeyValues:attributeDict];
    
    [self.videos addObject:v];
    
}

//某个元素解析完毕
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    
}

//开始解析文档
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    
}
//解析完毕
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    
}

GdataXML

  • 设置非ARC标记
JSON&XML_第1张图片
Snip20150713_335.png
JSON&XML_第2张图片
Snip20150713_336.png
JSON&XML_第3张图片
Snip20150713_340.png
  • 具体操作
//加载整个文档
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
//获得根节点
doc.rootElement
//获得其他节点
[element elementsForName:@"video"].stringValue
//获得节点的属性
[ele attributeForName:@"name"]
  • 实例代码
 // 加载整个文档
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
        
        // 获得所有video元素
        NSArray *elements = [doc.rootElement elementsForName:@"video"];
        for (GDataXMLElement *ele in elements) {
            XMGVideo *video = [[XMGVideo alloc] init];
            video.name = [ele attributeForName:@"name"].stringValue;
            video.url = [ele attributeForName:@"url"].stringValue;
            video.image = [ele attributeForName:@"image"].stringValue;
            video.length = [ele attributeForName:@"length"].stringValue.integerValue;
            
            [self.videos addObject:video];
        }

你可能感兴趣的:(JSON&XML)