2012-09-07 82 views
0

嗨我有以下json,我需要解析,但是,我努力解析內部數組。我目前只是打印每個內部數組,但我想打印每個標題並將標題添加到數組中。感謝您的任何幫助!iOS解析內部Json

JSON

{"nodes":[{ 
    "node":{ 
     "nid":"1420857", 
     "title":"Title 1", 
     "votes":"182", 
     "popular":"True", 
     "teaser":"Teaser 1" 
    }}, 
    {"node":{ 
     "nid":"1186152", 
     "title":"Title 2", 
     "votes":"140", 
     "popular":"True", 
     "teaser":"Teaser 2" 
    }}, 
    {"node":{ 
     "nid":"299856", 
     "title":"Title 3", 
     "votes":"136", 
     "popular":"True", 
     "teaser":"Teaser 3" 
    }} 
]} 

JSON分析器

NSError *error = nil; 
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.somefilename.json"]]; 
    if (jsonData) { 
     id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 
     if (error) { 
      NSLog(@"error is %@", [error localizedDescription]); 
      return; 
     } 
     NSArray *keys = [jsonObjects allKeys]; 
     for (NSString *key in keys) { 
      NSLog(@"%@", [jsonObjects objectForKey:key]); 
     } 
    } else { 
     // Handle Error 
    } 
+0

它的工作原理沒事,不正是應該做的。有什麼問題? –

+0

內心陣是什麼意思?您發佈的內容中只有一個數組,其他內容都是字典。你想要解析的究竟是什麼? – rdelmar

+0

就像我需要打印說每個標題在我的循環中我已經嘗試了很多東西,但不知道如何抓住我印刷內的那些對象。剛剛編輯我的問題對不起,我不清楚,但我幾乎只是在我的循環,而不是每個陣列打印每個標題 – user577732

回答

1

只是強制轉換它:

NSArray *nodes = (NSArray*)[jsonObjects objectForKey:@"nodes"]; 
for (NSDictionary *node in nodes){ 
    // do stuff... 
} 

方法,返回id(如-[objectForKey:],和-[objectAtIndex:])可以返回任何objective-c對象。您需要提前知道如何對其進行類型化以對其執行適當的操作。 JSON被轉換爲NSObject的當量:

  • object - >NSDictionary
  • array - >NSArray
  • string - >NSString
  • number - >NSNumber
  • boolean - >NSNumber
  • float - >NSNumber
  • null - >NSNull

能對各種NSNumbers區分,你必須調用相應類型的方法:-[intValue]-[boolValue]-[floatValue]。查看NSNumber docs瞭解更多信息。

+0

在做的東西評論我怎麼才能登錄標題 – user577732

+0

我試過NSLog(@「%@」,[node objectForKey:@「title」]);然而,這只是給我null – user577732

+0

它是一個嵌套的對象,所以你需要做[(NSDictionary *)[節點objectForKey:@「節點」] objectForKey:@「標題」]; – Jeff

0

你可以用我的方法JSON解析,

Parse方法:

-(void)jsonDeserialize:(NSString *)key fromDict:(id)content completionHandler:(void (^) (id parsedData, NSDictionary *fromDict))completionHandler{ 
    if (key==nil && content ==nil) { 
     completionHandler(nil,nil); 
    } 
    if ([content isKindOfClass:[NSArray class]]) { 
     for (NSDictionary *obj in content) { 
      [self jsonDeserialize:key fromDict:obj completionHandler:completionHandler]; 
     } 
    } 
    if ([content isKindOfClass:[NSDictionary class]]) { 
     id result = [content objectForKey:key]; 
     if ([result isKindOfClass:[NSNull class]] || result == nil) { 
      NSDictionary *temp = (NSDictionary *)content; 
      NSArray *keys = [temp allKeys]; 
      for (NSString *ikey in keys) { 
      [self jsonDeserialize:key fromDict:[content objectForKey:ikey] completionHandler:completionHandler]; 
      } 
     }else{ 
      completionHandler(result,content); 
     } 
    } 
} 

方法調用:

NSData *content = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Sample" ofType:@"json"]]; 
    NSError *error; 

//獲取序列化JSON數據...

id dictionary = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:&error]; 

//獲取關鍵數據稱爲的GetInfo

 [self jsonDeserialize:@"GetInfo" fromDict:dictionary completionHandler:^(id parsedData, NSDictionary *fromDict) { 
      NSLog(@"%@ - %@",parsedData,fromDict); 
     }];