在ios中获取Json数据[关闭]
Posted
技术标签:
【中文标题】在ios中获取Json数据[关闭]【英文标题】:Get the Json Data in ios [closed] 【发布时间】:2013-08-29 10:36:01 【问题描述】:我正在从 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"];
【问题讨论】:
你遇到了什么错误??您是否将 json 存储在数组中? 是的,我已将数据存储在数组中。但我想写 for 循环来检查条件 【参考方案1】:这是我的解决方案:
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"];
【讨论】:
【参考方案2】:为什么大家都推荐第三方框架? 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
【讨论】:
以上是关于在ios中获取Json数据[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Swift 创建 JSON Codable [关闭]
在 iOS 应用程序中获取和解析 JSON 的最佳方法? [关闭]