2013-02-15 99 views
-3

我試圖從目標c中的Twitter API獲取一些信息。從JSON數組中獲取內容

這是我的代碼來解析JSON和獲取信息:

-(void)getJSONData { 

    // Create new SBJSON parser object 
    SBJsonParser *parser = [[SBJsonParser alloc] init]; 

    // Prepare URL request to download statuses from Twitter 
    NSString *url = [[NSString alloc] initWithFormat:@"http://search.twitter.com/search.json?geocode=52.13263,5.29127,1000km&result_type=recent&lang=nl&q=twitter&rpp=1"]; 

    NSURL *scriptUrl = [NSURL URLWithString:url]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:scriptUrl]; 

    // Perform request and get JSON back as a NSData object 
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    // Get JSON as a NSString from NSData response 
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

    NSString *combine = [[NSString alloc] initWithFormat:@"%@%@%@", @"[", json_string, @"]"]; 

    // Here we're using NSArray since we're parsing an array of JSON status objects 
    NSArray *statuses = [parser objectWithString:combine]; 

    // Each element in statuses is a single status 
    // represented as a NSDictionary 
    for (NSDictionary *status in statuses) { 

     NSLog(@"%@", [status objectForKey:@"results"]); 

    } 

} 

在控制檯中我得到這個回:

2013-02-15 21:23:06.294 JSON[9585:c07] (
     { 
     "created_at" = "Fri, 15 Feb 2013 20:22:45 +0000"; 
     "from_user" = "Utrecht_krant"; 
     "from_user_id" = 465499012; 
     "from_user_id_str" = 465499012; 
     "from_user_name" = "Utrecht Nieuws"; 
     geo = "<null>"; 
     id = 302513299378036736; 
     "id_str" = 302513299378036736; 
     "iso_language_code" = nl; 
     location = Utrecht; 
     metadata =   { 
      "result_type" = recent; 
     }; 
     "profile_image_url" = "http://a0.twimg.com/profile_images/1759014211/Utrecht_normal.gif"; 
     "profile_image_url_https" = "https://si0.twimg.com/profile_images/1759014211/Utrecht_normal.gif"; 
     source = "&lt;a href=&quot;http://twitterfeed.com&quot;&gt;twitterfeed&lt;/a&gt;"; 
     text = "#Utrecht GO Prinselijk paar per trein naar Lech - NOS: Reformatorisch DagbladPrinselijk paar per trein naar Lech... http://t.co/SL1aNaKe"; 
     "to_user" = "<null>"; 
     "to_user_id" = 0; 
     "to_user_id_str" = 0; 
     "to_user_name" = "<null>"; 
    } 
) 

這使我從對象的信息:"results"。現在是我的問題:如何從陣列"results"獲取對象?所以如果我想得到"from_user"我如何在Objective C中需要這個?

在此先感謝。

+2

他們是字典,來吧男:objectForKey: – CodaFi 2013-02-15 20:41:00

+0

你看到評論«/ /狀態中的每個元素是一個單一的狀態 //表示爲一個NSDictionary»在**你的**代碼? – 2013-02-15 20:44:46

+1

@Sebastiaan:*什麼*錯誤,告訴我們*什麼是錯的,所以我們可以幫助。你不能去找汽車修理工說「我聽到噪音,怎麼了?」 – dreamlax 2013-02-16 03:54:32

回答

0

你吐出的NSLog是一個包含單個字典(你可以從大括號中得知)的數組(你可以從封閉圓括號中得知)。所以,你可以使用數組和字典方法,深入到你想要的數據:

NSArray *result = [status objectForKey:@"results"]; 
NSLog(@"%@", [[result objectAtIndex:0] objectForKey:@"from_user"]); 

雖然你可能想,如果你不知道數據是100%保證在此添加一些健全檢查格式。

+0

謝謝!這就像一個魅力! – Sebastiaan 2013-02-15 20:51:01