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; //显示最右边的箭头

 

posted on   高彰  阅读(464)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示