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

时间:2013-04-03 19:28:10

标签: iphone ios ipad

我必须从Web服务检索数据并存储到表视图中。 我有一个框架,但是对我来说无法工作here

此处使用的问题是不允许创建对象的创建对象。

谢谢

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

检查此代码

@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;
 }