[英]Parsing Complex JSON (Navigating the hierarchy of objects it produces) in Objective C
我正在開發基於學校菜單和菜品的應用程序,其中卡住的是解析復雜/嵌套的json
示例Json URL: Json數據
到目前為止,我已經按照下面的代碼所述使用SBJSON完成了此操作 :
dispatch_async(dispatch_get_main_queue(),^ {
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
[self.view setUserInteractionEnabled:YES];
[HUD hideUIBlockingIndicator];
}
else
{
Url =[NSString stringWithFormat:@"http://private-e8e699-yumyummi.apiary-mock.com/districts/115/schools/43/menus/"];
checkData =[[NSMutableArray alloc]init];
URLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[Url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:URLRequest returningResponse:&response error:&error];
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
SBJsonParser* parser = [[SBJsonParser alloc] init];
NSError *jsonError;
if(jsonError == nil)
{
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
NSLog(@"%@",json);
NSDictionary *data = [json objectForKey:@"data"];
}
});
如果您的問題是您不知道json中數據的確切結構,則可以使用以下方法:
if([json isKindOfClass:[NSDictionnary class]) {
// actions for a dictionnary
id branch = json[@"your key"];
} else if([json isKindOfClass:[NSArray class]) {
// actions for an array
id branch = json[index];
}
另外, if(jsonError == nil)
沒有意義,您可以在執行[NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.