2013-08-29 38 views
0

我从Jason URl获取了一些医生的数据。该数据显示了一些医生的单一医生位置,并显示了一些医生的多个位置。我如何显示该数据。一旦软件给我的解决方案该数据显示如下在ios中获取Json数据

"id":"135","speciality":"Allergy","type":"P", 
    "doc_name":"Patrick M. Ambrosio, D.O., F.A.C.A.A.I.", 
    "doc_profile_url":"Patrick-M-Ambrosio", "doc_offices":[ 
      { 
       "office_id":17, 
       "office_name":"Old Bridge", 
       "office_url":"Old-Bridge", 
       "state":"New Jersey", 
       "state_short":"NJ", 
       "city":"Old-Bridge", 
       "lat":"40.3975", 
       "lon":"-74.3298", 
       "office_combined_name":"Old Bridge,New Jersey,NJ" 
      }, 
      { 
       "office_id":7, 
       "office_name":"Woodbridge", 
       "office_url":"Woodbridge", 
       "state":"New Jersey", 
       "state_short":"NJ", 
       "city":"Iselin", 
       "lat":"40.555", 
       "lon":"-74.3151", 
       "office_combined_name":"Woodbridge,New Jersey,NJ" 
      } ] 
      }, { 
      "id":"2", "speciality":"Allergy", "type":"P", "doc_name":"Ricardo Arayata, M.D., F.A.C.A.A.I.", 
    "doc_profile_url":"ricardo-arayata-md", "doc_offices":[ 
      { 
       "office_id":22, 
       "office_name":"Purchase", 
       "office_url":"Purchase", 
       "state":"New York", 
       "state_short":"NY", 
       "city":"Purchase", 
       "lat":"41.0099", 
       "lon":"-73.6959", 
       "office_combined_name":"Purchase,New York,NY" 
      }, 
      { 
       "office_id":15, 
       "office_name":"New Rochelle", 
       "office_url":"New-Rochelle", 
       "state":"New York", 
       "state_short":"NY", 
       "city":"New-Rochelle", 
       "lat":"40.9158", 
       "lon":"-73.7864", 
       "office_combined_name":"New Rochelle,New York,NY" 
      } ] 
      }, { 
      "id":"3", "speciality":"ENT", "type":"P", "doc_name":"Anna Aronzon, M.D.", 
    "doc_profile_url":"anna-aronzon-md", "doc_offices":[ 
      { 
       "office_id":27, 
       "office_name":"Wall Street", 
       "office_url":"Wall-Street", 
       "state":"New York", 
       "state_short":"NY", 
       "city":"New-York", 
       "lat":"40.7096", 
       "lon":"-74.0104", 
       "office_combined_name":"Wall Street,New York,NY" 
      } ] 
      }, { 
      "id":"4", "speciality":"ENT", "type":"P", "doc_name":"Jonathan Aviv, M.D., F.A.C.S.", 
    "doc_profile_url":"jonathan-aviv-md", "doc_offices":[ 
      { 
       "office_id":23, 
       "office_name":"Sleepy Hollow", 
       "office_url":"Sleepy-Hollow", 
       "state":"New York", 
       "state_short":"NY", 
       "city":"Sleepy-Hollow", 
       "lat":"41.0802", 
       "lon":"-73.8572", 
       "office_combined_name":"Sleepy Hollow,New York,NY" 
      }, 
      { 
       "office_id":6, 
       "office_name":"East Side", 
       "office_url":"East-Side", 
       "state":"New York", 
       "state_short":"NY", 
       "city":"New-York", 
       "lat":"40.7768", 
       "lon":"-73.9541", 
       "office_combined_name":"East Side,New York,NY" 
      } ] 
      }, { 
      "id":"163", "speciality":"ENT", "type":"P", "doc_name":"Andrew Azer, M.D.", 
    "doc_profile_url":"andrew-azer-md", "doc_offices":[ 
      { 
       "office_id":17, 
       "office_name":"Old Bridge", 
       "office_url":"Old-Bridge", 
       "state":"New Jersey", 
       "state_short":"NJ", 
       "city":"Old-Bridge", 
       "lat":"40.3975", 
       "lon":"-74.3298", 
       "office_combined_name":"Old Bridge,New Jersey,NJ" 
      }, 
      { 
       "office_id":7, 
       "office_name":"Woodbridge", 
       "office_url":"Woodbridge", 
       "state":"New Jersey", 
       "state_short":"NJ", 
       "city":"Iselin", 
       "lat":"40.555", 
       "lon":"-74.3151", 
       "office_combined_name":"Woodbridge,New Jersey,NJ" 
      } ] 

和我给帽子代码也一次plaese检查了这一点。

- offC = [[[rr objectForKey:@"doc_offices"] 
      objectAtIndex:0]objectForKey:@"office_name"]; 
NSString *docOfficeID 
      = [[[rr objectForKey:@"doc_offices"]objectAtIndex:0]objectForKey:@"office_id"]; 
+1

什么错误是U越来越??你是否将json存储在数组中? –

+1

是的,我已经将数据存储在数组中。但我想写循环检查条件 –

回答

0

为什么大家都推荐第三方框架? iOS拥有本机JSON序列化工具:NSJSONSerialization。这非常容易使用。例如:

dispatch_queue_t fetchQueue = dispatch_queue_create("nameOfYourQ", NULL); //here i create dispatch for connection (do not block UI!) 
dispatch_async(fetchQueue, ^{ // block to fetch and parse 
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://yourlink"]]; // NSData - connection for JSON 
    id arrayOrDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; // parse NSData object to Dictionaries and Arrays. 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [yourTableView reloadData]; // reload your view on main thread. 
    }); 
}); 

这是一个非常简单的例子。我想要做的是使用NSURLConnectionDelegate下载数据,然后使用代理方法使用NSJSONSerialization解析数据。

等一下,在您的JSON结构中,您有多个doc_offices键。所以我认为你真的有(得到office_id) array - dictionary - dictionary - array。

所以:

NSNumber *docOfficeID = [[[[rr objectAtIndex:0] objectForKey:@"doc_offices"] objectAtIndex:0] objectForKey:@"office_id"]; 

这是不是一个NSString。这是NSNumber

2

这里是我的解决方案:

 NSError *jsonParsingError = nil; 
     NSMutableArray *arrDoctorInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError]; 
     for (NSMutableDictionary *data in arrDoctorInfo) { 

      NSMutableArray *arrOffice = [data valueForKey:@"doc_offices"]; 
      NSString *strID = [data valueForKey:@"ID"]; 
      . 
      . 
      NSString *strdoc_profile_url = [data valueForKey:@"doc_profile_url"]; 
      for (NSMutableDictionary *dictDoffice in arrOffice) { 
       NSString *strdictDoffice = [dictDoffice valueForKey:@"office_id"]; 
       . 
       . 
       NSString *stroffice_combined_name = [dictDoffice valueForKey:@"office_combined_name"]; 
      } 

     }