我从Jason URl那里得到了一些医生的数据。该数据显示了一些医生的单一医生位置,并显示了一些医生的多个位置。我该如何显示该数据。一旦plaese给我解决方案,数据显示在下面
"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"];
答案 0 :(得分: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"];
}
}
答案 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
)
数组 - 字典 - 字典 - 数组。
所以:
NSNumber *docOfficeID = [[[[rr objectAtIndex:0] objectForKey:@"doc_offices"] objectAtIndex:0] objectForKey:@"office_id"];
这不是NSString
。这是NSNumber