2013-04-20 67 views
0

我有這個從cms返回的json,我需要將它分開並使用不同的值。在目標C中解析JSON

{"nodes": 
    [{"node": 
     {"created":"14012013165204","lastupdated":"03042013133901","type":"News"} 
    }] 
} 

我用JSONKit框架,這裏是我的代碼:

NSString* theURL = [NSString stringWithFormat:@"http://testserver.com/content_lastupdate.json"]; 
    NSError* err = nil; 
    NSURLResponse* response = nil; 
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init]; 
    NSURL*URL = [NSURL URLWithString:theURL]; 
    [request setURL:URL]; 
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
    [request setTimeoutInterval:30]; 
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

    NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSDictionary *resultsDictionary = [output objectFromJSONString]; 

    NSLog([NSString stringWithFormat:(@"Response: %@"), output]); 

    NSDictionary *nodes = [resultsDictionary objectForKey:@"nodes"]; 
    NSObject *node = [nodes objectForKey:@"node"]; 

我不知道如何得到結果的一個部分。找到節點密鑰,但下一個不是。任何想法什麼基礎我在這裏失蹤?

+0

A)學習如何閱讀JSON - 這非常簡單。查看JSON.org。 B)使用NSLog。它使用類似JSON的語法打印解碼的JSON對象(儘管使用'()'而不是'[]'),因此您可以在「剝離」每個圖層時看到結構。 – 2013-04-20 11:54:06

回答

3

「節點」是一個數組。試試這個:

NSArray *nodes = resultsDictionary[@"nodes"]; 
NSDictionary *node = nodes[0]; 
node = node[@"node"]