如何在我的应用程序中解析/获取JSON

时间:2014-02-03 06:37:42

标签: php ios objective-c json xcode

我有数据库(linkP

如何解析上述链接中的所有数据并显示结果

2 个答案:

答案 0 :(得分:2)

- (void)viewDidLoad
{
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://arproject.site90.com/jsonbuilding.php"]];

    [request setHTTPMethod:@"GET"];

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

    NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

    NSArray *array=[jsonArray objectForKey:@"result"];

    for (int i=0; i<[array count]; i++) {
        NSLog(@"the building id==%@",[[array objectAtIndex:i]objectForKey:@"building_id"]);
        NSLog(@"the building name==%@",[[array objectAtIndex:i]objectForKey:@"building_name"]);
        NSLog(@"the latitude==%@",[[array objectAtIndex:i]objectForKey:@"latitude"]);
         NSLog(@"the longitude==%@",[[array objectAtIndex:i]objectForKey:@"longitude"]);
         NSLog(@"the picture==%@",[[array objectAtIndex:i]objectForKey:@"picture"]);
}

答案 1 :(得分:1)

dispatch_async(kBgQueue, ^{

    NSURL *myURL = [NSURL URLWithString:@"http://arproject.site90.com/jsonbuilding.php"];
    NSData *data=[NSData dataWithContentsOfURL:myURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

-(void)fetchedData : (NSData*)responseData
{
      NSError *error;

//parsing json data


 NSMutableArray *dataArray;
 NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

if(error == nil)
{

    dataArray =[dictionary valueForKey:@"result"];

  for (int i=0; i<[dataArray count]; i++)
  {
    NSLog(@"%@",dataArray);
   } 

}
if(error)
NSLog(@"Error is %@",[error localizedDescription]);
}