0

我从 Jason URl 获得了一些医生的数据。该数据显示了一些医生的单个医生位置,并显示了一些医生的多个位置。我怎样才能显示该数据。一旦请给我解决方案该数据显示在下面

 "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"
          }    ]

我也给了帽子代码,请检查一下。

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

2 回答 2

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"];
            }

        }
于 2013-08-29T11:33:20.770 回答
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)数组 - 字典 - 字典 - 数组。

所以:

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

这不是一个NSString. 这是NSNumber

于 2013-08-29T11:22:22.453 回答