【问题标题】:how to parsing data in JSON to tableview如何将 JSON 中的数据解析为 tableview
【发布时间】:2014-06-27 04:26:21
【问题描述】:

我有一个 json http://vmg.hdvietpro.com/ztv/home 以及如何在此链接中解析数据 json 到表视图。我尝试了这段代码,但我无法从 json 获取数据到我的应用程序。感谢您的帮助

电影.h

@interface Movies : NSObject
@property (strong,nonatomic) NSString *moviesId;
@property (strong,nonatomic) NSString *text1;
@property (strong,nonatomic) NSString *thumbnail;
#pragma mark -
#pragma mark Class Methods
-(id)initWithMoviesName:(NSString *)cText1 andThumbnail:(NSString*) cThum  andMoviesID:(NSString*) cID;

@end

电影.m

@implementation Movies
@synthesize text1,moviesId,thumbnail;
-(id)initWithMoviesName:(NSString *)cText1 andThumbnail:(NSString*) cThum  andMoviesID:(NSString    *) cID{
    self=[super init];
    if (self) {
        text1=cText1;
        thumbnail=cThum;
        moviesId=cID;
    }
return self;
}
@end

表视图控制器

#define getDataURL @"http://vmg.hdvietpro.com/ztv/home"
  -(void) retrieveData{
        NSURL *url=[NSURL URLWithString:getDataURL];
        NSData *data=[NSData dataWithContentsOfURL:url];
        jsonArray=[NSJSONSerialization JSONObjectWithData:data
                                                  options:kNilOptions error:nil];
        moviesArray=[[NSMutableArray alloc] init];
        for (int i=0; i<jsonArray.count; i++) {
            NSString *cID=[[jsonArray objectAtIndex:i] objectForKey:@"id"];
            NSString *cName=[[jsonArray objectAtIndex:i] objectForKey:@"text1"];
            NSString *cThum=[[jsonArray objectAtIndex:i] objectForKey:@"thumbnail"];
            [moviesArray addObject:[[Movies alloc]initWithMoviesName:cName andThumbnail:cThum andMoviesID:cID]];
        }
        [self.tableView reloadData];
    }

【问题讨论】:

  • “我有一个 json vmg.hdvietpro.com/ztv/home 以及如何解析数据”不是一句话。请用英文写。
  • @Time To Learn 是您的确切问题,您是否收到任何错误也表明该错误。
  • 这是我运行应用程序时的错误 2014-06-27 11:13:48.707 ZingTVIOS[5639:60b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8d11a70 2014-06-27 11 :13:48.796 ZingTVIOS [5639:60b] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例0x8d11a70'
  • @Time To Learn 我已经给出了解决此问题的代码,请检查

标签: ios json uitableview


【解决方案1】:

尝试关注:

NSArray * respArray = [[[[json objectForKey:@"response"]objectForKey:@"hot_program"]objectForKey:@"itemPage"]objectForKey:@"page"];

【讨论】:

    【解决方案2】:

    尝试做这样的事情。这不是确切的解决方案,您必须按自己的方式解决。

    NSString *link =  [NSString stringWithFormat:@"yourServiceURL"];
        NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url=[NSURL URLWithString:encdLink];
        NSData *response = [NSData dataWithContentsOfURL:url];
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];
    

    现在您将数据保存在数组中,并使用objectForKeyvalueForKey 将其提取出来。

    这就是我从 JSON 中获取图像并显示在我的应用程序中的操作。首先从上面的代码中获取数组。然后像这样提取图像内容:

    _demoArray = [json valueForKey:@"ContentImage"];
    

    现在你的 demoArray 中有值了。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      我觉得它对你有用。试试这种方式

          #import "ViewController.h"
      
      @interface ViewController ()
      {
          NSMutableData *webData;
          NSURLConnection *connection;
          NSMutableArray *array;
      }
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
      
      
          [[self TableView]setDataSource:self];
          array=[[NSMutableArray alloc]init];
      
      
          NSURL *url=[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"];
          NSLog(@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json");
          NSURLRequest *req=[NSURLRequest requestWithURL:url];
          connection=[NSURLConnection connectionWithRequest:req delegate:self];
          if(connection)
          {
              NSLog(@"Connected");
              webData=[[NSMutableData alloc]init];
          }
      
      
      }
      
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
      {
          [webData setLength:0];
      
      }
      -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
      {
          [webData appendData:data];
      
      }
      -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
      {
          NSLog(@"Error is");
      }
      -(void)connectionDidFinishLoading:(NSURLConnection *)connection
      {
          NSDictionary *all=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
      
         // NSString *label=[all objectForKey:@"lable" ];
         // [array addObject:label];
        NSDictionary *feed=[all objectForKey:@"feed"];
      
          NSArray *arrayOFEntry=[feed objectForKey:@"entry"];
      
          for (NSDictionary *diction in arrayOFEntry) {
              NSDictionary *title=[diction objectForKey:@"title"];
              NSString *label=[title objectForKey:@"label"];
              [array addObject:label];
      
          }NSLog(@"good mrng");
          [[self TableView]reloadData];
      }
      
      - (IBAction)getTopMoves:(id)sender
      {
          NSLog(@"good mrng");
      
      
      }
      -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
          return 1;
      }
      -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          return [array count];
      }
      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
          static NSString *CellIdentifier = @"Cell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (!cell)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
          }
          cell.textLabel.text = [array objectAtIndex:indexPath.row];
          return cell;
          return cell;
      }
      
      
      
      @end
      

      【讨论】:

        【解决方案4】:

        试试这个...

         -(void) retrieveData1
        {
            NSURL * url = [NSURL URLWithString:getDataURL1];
        
            NSData * data = [NSData dataWithContentsOfURL:url];
        
            id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        
            _moviesArray=[[NSMutableArray alloc] init];
            _moviesArray1=[[NSMutableArray alloc] init];
            _moviesArray2=[[NSMutableArray alloc] init];
        //  NSArray * respArray = [[[[json objectForKey:@"response"]objectForKey:@"hot_program"]objectForKey:@"itemPage"]objectForKey:@"page"];
            NSDictionary *dic=[json objectForKey:@"response"];
            NSDictionary *dic1= [dic objectForKey:@"hot_program"];
            NSDictionary *dic2=[dic1 objectForKey:@"itemPage"];
            for (NSDictionary *diccc in [dic2 objectForKey:@"page"])
            {
                NSLog(@"%@",diccc);
        [self.moviesArray addObject:[diccc objectForKey:@"id"]];
        
                [self.moviesArray1 addObject:[diccc objectForKey:@"text1"]];
                [self.moviesArray2 addObject:[diccc objectForKey:@"thumbnail"]];
        
        
            }
        
              NSLog(@"%@",self.moviesArray);
            NSLog(@"%@",self.moviesArray1);
            NSLog(@"%@",self.moviesArray2);
        
         //  [self.tableView reloadData];
        
        }
        

        这是解析数据的一种方法,有很多方法可以解析..

        NSArray * respArray = [[[[json objectForKey:@"response"]objectForKey:@"hot_program"]objectForKey:@"itemPage"]objectForKey:@"page"];
            NSLog(@"%@",respArray);
            NSArray *respArray1= [[[[json objectForKey:@"response"]objectForKey:@"new_video"]objectForKey:@"itemPage"]objectForKey:@"page"];;
            NSLog(@"%@",respArray1);
            NSArray *respArray2= [[json objectForKey:@"response"]objectForKey:@"hot_program_by_category"];
            NSLog(@"%@",respArray2);
            for (NSDictionary *dic in [[respArray2 valueForKey:@"itemPage"]valueForKey:@"page"])
            {
                NSLog(@"%@",dic);
            }
        

        别忘了重新加载一个 tableview..我在那里评论只是取消评论它

        【讨论】:

          猜你喜欢
          • 2015-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-05
          • 1970-01-01
          • 1970-01-01
          • 2020-04-29
          • 1970-01-01
          相关资源
          最近更新 更多