2011-08-31 94 views
1

即時得到的JSON響應到我的應用[Twitter的web服務],是一個字符串,但是,例如索引0處的對象是:IOS陣列到字典或陣列

es array en:0, tiene { 
contributors = "<null>"; 
coordinates = "<null>"; 
"created_at" = "Thu Aug 04 23:26:05 +0000 2011"; 
favorited = 0; 
geo = "<null>"; 
id = 99259843982016513; 
"id_str" = 99259843982016513; 
"in_reply_to_screen_name" = "<null>"; 
"in_reply_to_status_id" = "<null>"; 
"in_reply_to_status_id_str" = "<null>"; 
"in_reply_to_user_id" = "<null>"; 
"in_reply_to_user_id_str" = "<null>"; 
place = "<null>"; 
"possibly_sensitive" = 0; 
"retweet_count" = 0; 
retweeted = 0; 
source = "<a href=\"http://twitter.com/tweetbutton\" rel=\"nofollow\">Tweet Button</a>"; 
text = "Stack Exchange Q&A site proposal: Freelance Workers http://t.co/yaW1RHp"; 
truncated = 0; 
user =  { 
    "contributors_enabled" = 0; 
    "created_at" = "Mon Jul 13 19:39:31 +0000 2009"; 
    "default_profile" = 0; 
    "default_profile_image" = 0; 
    description = "My goal is to enable the brain computer interfaces to use the possibilities of mobile platforms for robotics and physical computing"; 
    "favourites_count" = 0; 
    "follow_request_sent" = "<null>"; 
    "followers_count" = 92; ... 

所以有像17我的數組[對於每個twitt]的對象,那麼如何將這些對象分解爲更多的數組或字典?

我specifaclly想要的文字鍵

text = "Apple vs Samsung tablets [haha and Samsung is an Apple hardware provider!!]\nhttp://t.co/rvv43Hy"; 

非常感謝

+1

(第一個)「text」值很容易,因爲它位於外部字典中。只需使用'objectForKey:@「文本」'。 (但是你爲「文本」顯示了不同的值,因此它可能是更深入的嵌入版本。) –

回答

3

可能已經有一個爲此而構建的解析器,但如果沒有,我認爲您會發現以下方法有用。

NSArray *strings = [input componentsSeparatedByString:@";"]; 

它返回一個字符串數組(在本例中)「;」作爲分隔符。

{貢獻者= 「」,座標= 「」,...}

您可以將它們進一步分離他們:

NSDictionary *dict = [NSDictionary dictionary]; 
for (NSString *s in strings) 
{ 
    NSArray *keyValue = [s componentsSeparatedByString:@"="]; 
    NSString *key = [keyValue objectAtIndex:0]; 
    NSString *value = [keyValue objectAtIndex:1]; 
    [dict setValue:value forKey:key]; 
} 

有似乎是一些額外的數據響應的開始,你可能不得不首先解決這個問題。

+0

OP的數據是包含另一個NSDictionary的NSDictionary。試圖用'componentsSeparateByStrings'解析它是他的愚蠢之處。 –

2

你會的,例如,從你的字典外提取「用戶」對象並將其賦值給一個NSDictionary變量。然後,您可以從第二個字典中提取「default_profile」。

可以編寫一個簡單的「路徑導航器」工具,通過「路徑符號」訪問單個實體,而不必明確提取組件,但我不知道「罐頭」。

1

以防萬一有人在找這個,我結束了使用

開源JSON框架由斯蒂格Brautaset。 及其解析器, 並遵循一些指令f rom here