我想在ios6中添加json框架并使用它如何从服务器获取数据



我必须从web-service检索数据并存储到表视图。我有一个框架,但它不能为我工作,在这里

这里的问题是不能创建创建对象。

谢谢

您不需要创建json.h文件的对象。您只需要导入json.h文件,然后通过[responseString JSONValue]使用它。

检查此代码

@interface videoViewController ()
{ 
  NSMutableData *webData;
  NSURLConnection *connection;
  NSMutableArray *array;
  NSMutableArray *array2;  
}
- (void)viewDidLoad
{
  [super viewDidLoad];
  array=[[NSMutableArray alloc]init];
  array2=[[NSMutableArray alloc]init];
  NSURL *url=[NSURL URLWithString:@"http://localhost/videosphp/videoname2.php"];
  NSURLRequest *request=[NSURLRequest requestWithURL:url];
  connection=[NSURLConnection connectionWithRequest:request delegate:self];
  if(connection)
   {
     webData=[[NSMutableData alloc]init];
   }
}

-(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
{
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Unable to    connect internet. Please check your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  [alert show];
  return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  //use NSDictionary if the data in dictionary
  NSArray *allDataArray=[NSJSONSerialization JSONObjectWithData:webData options:0    error:nil];
  for (NSDictionary *diction in allDataArray) 
  {
    NSString *videoname=[diction objectForKey:@"videoname"];
    [array addObject:videoname]; 
  }
  [[self tableview]reloadData];
}
-(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:UITableViewCellAccessoryDisclosureIndicator     reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    return cell;
 }

最新更新