zoukankan      html  css  js  c++  java
  • Json/Xml数据解析 生成plist文件

     1 #pragma mark --Json解析第一种
     2 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     3 //    0.请求路径
     4     NSURL *url=[NSURL URLWithString:@"http://mobile.ximalaya.com/mobile/v1/album/track?albumId=203&device=iPhone&isAsc=true&pageId=1&pageSize=20&statEvent=pageview%2Falbum%40203&statModule=suggest"];
     5 //    1.创建请求对象
     6     NSURLRequest *request=[NSURLRequest requestWithURL:url];
     7 //    2.发送请求
     8     [NSURLConnection sendAsynchronousRequest:request
     9                                        queue:[[NSOperationQueue alloc]init]
    10                            completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
    11 //                解析json
    12                 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    13                 NSLog(@"%@",dict);
    14     }];
    15 }

    运行结果如下:

     

     1 - (void)viewDidLoad {
     2     [super viewDidLoad];
     3     self.title=@"解析列表";
     4     self.view.backgroundColor=[UIColor blueColor];
     5 
     6 #pragma mark json解析第二种
     7     dispatch_async(dispatch_queue_create("", DISPATCH_QUEUE_SERIAL), ^{
     8         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://mobile.ximalaya.com/mobile/v1/album/track?albumId=203&device=iPhone&isAsc=true&pageId=1&pageSize=20&statEvent=pageview%2Falbum%40203&statModule=suggest"]];
     9         if (data) {
    10             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    11             dispatch_async(dispatch_get_main_queue(), ^{
    12                 NSLog(@"%@",dic);
    13             });
    14         }
    15     });
    16 }

    结果如下:

    编辑生成plist文件

     1 #pragma mark --Json解析
     2 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     3 //    0.请求路径
     4     NSURL *url=[NSURL URLWithString:@"http://mobile.ximalaya.com/mobile/v1/album/track?albumId=203&device=iPhone&isAsc=true&pageId=1&pageSize=20&statEvent=pageview%2Falbum%40203&statModule=suggest"];
     5 //    1.创建请求对象
     6     NSURLRequest *request=[NSURLRequest requestWithURL:url];
     7 //    2.发送请求
     8     [NSURLConnection sendAsynchronousRequest:request
     9                                        queue:[[NSOperationQueue alloc]init]
    10                            completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
    11 //                解析json
    12                 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    13                 NSLog(@"%@",dict);
    14 //      编辑生成plist文件
    15                                [dict writeToFile:@"/Users/dspan/Documents/Examples_of_Buddhism.plist" atomically:YES];
    16     }];
    17 }

    用Tableview显示数据

    ViewController.h
    1 #import <UIKit/UIKit.h>
    2
    3 @interface ViewController : UITableViewController
    4
    5 @end
     
    ViewController.m
     1 #import "ViewController.h"
     2 #import <AFNetworking.h>
     3 #import <UIImageView+WebCache.h>
     4 @interface ViewController ()
     5 /*视频数据*/
     6 @property(nonatomic,strong)NSArray *videos;
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad {
    12     [super viewDidLoad];
    13     self.title=@"解析列表";
    14     self.view.backgroundColor=[UIColor orangeColor];
    15 //    0.请求路径
    16     NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/video"];
    17     //    1.创建请求对象
    18     NSURLRequest *request=[NSURLRequest requestWithURL:url];
    19     //    2.发送请求
    20     [NSURLConnection sendAsynchronousRequest:request
    21                                        queue:[[NSOperationQueue alloc]init]
    22                            completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
    23                                //                解析json
    24                                NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    25                                NSLog(@"%@",dict);
    26                                //获得视频数组
    27                                self.videos=dict[@"videos"];
    28 //                               刷新表格
    29                                [self.tableView reloadData];
    30                                //      编辑生成plist文件
    31                                //                               [dict writeToFile:@"/Users/dspan/Documents/Video.plist" atomically:YES];
    32     }];
    33 }
    34 #pragma mark - 数据源方法
    35 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    36     return self.videos.count;
    37 }
    38 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    39     static NSString *ID=@"video";
    40     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    41     
    42     NSDictionary *video=self.videos[indexPath.row];
    43     cell.textLabel.text=video[@"name"];
    44     cell.detailTextLabel.text=[NSString stringWithFormat:@"时长:%@",video[@"length"]];
    45    //    NSString *image=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:image];//显示占位图
    46     NSString *image=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:video[@"image"]]; //显示网络数据图
    47     [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1"]];//占位图片名称1.png
    48     return cell;
    49 }

    效果图:

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头

  • 相关阅读:
    使用Kubeadm创建k8s集群之节点部署(三十一)
    Jmeter之Bean shell使用(二)
    Jmeter之Bean shell使用(一)
    Jmeter之JDBC Request使用方法(oracle)
    Jmeter调试工具---HTTP Mirror Server
    Jenkins快速上手
    Jmeter之逻辑控制器(Logic Controller)
    Jmeter属性和变量
    Jmeter之HTTP Request Defaults
    Jmeter之Http Cookie Manager
  • 原文地址:https://www.cnblogs.com/gaozhang12345/p/5903342.html
Copyright © 2011-2022 走看看