2013-04-22 90 views
0

Web服務調用返回一個JSON響應,並將其放入一個NSMutableArray。 JSON響應看起來是這樣的,從NSMutableArray創建一個NSMUtableDictionary

(
     { 
     DispName = "Jonny Depp (Marvel Comics)"; 
     "Int_Adr" = 273; 
     "Int_Group" = 0; 
    }, 
     { 
     DispName = "Mahendra Singh Dhoni (Indian Premier League)"; 
     "Int_Adr" = 265; 
     "Int_Group" = 0; 
    }, 
     { 
     DispName = "Otara De Mel (ODEL UNLIMITED)"; 
     "Int_Adr" = 496; 
     "Int_Group" = 0; 
    }, 
     { 
     DispName = "Rahul Dravid (Indian Premier League)"; 
     "Int_Adr" = 266; 
     "Int_Group" = 0; 
    } 
) 

現在我想創建另一個NSMutableDictionary只包含鍵,DispNameInt_Adr

所以我正在做這樣的事情。我已經在這個.h文件中聲明瞭一個NSMutableDictionary

@property (nonatomic, strong) NSMutableDictionary *recipients; 

在.m文件,

// get the JSON response and put it in an array 
NSMutableArray *contactsArray = [jsonParser objectWithString:response]; 
NSLog(@"%@, array count = %d", contactsArray, contactsArray.count); // the count is 50 

//[self.recipients removeAllObjects]; 
for (NSDictionary *item in contactsArray) { 
    [self.recipients setObject:[item objectForKey:@"Int_Adr"] forKey:@"Int_Adr"]; 
    [self.recipients setObject:[item objectForKey:@"DispName"] forKey:@"DispName"]; 
} 
NSLog(@"%@, array count = %d", self.recipients, self.recipients.count); // the count shows 2! 

我設置只有2個值給收件人字典。但是當我記錄輸出時,它只顯示最後一組。

{ 
    DispName = "Rahul Dravid (Indian Premier League)"; 
    "Int_Adr" = 266; 
} 

我該如何解決這個問題?

謝謝。

回答

2

這是我對你的建議,使用NSMutableArray連同NSMutableDictionary。 JSON表示它是字典數組。你必須重新設計你的代碼。

@property (nonatomic, strong) NSMutableArray *recipients; //not NSMutableDictionary 

//Store 
for (NSDictionary *item in contactsArray) { 
    NSMutableDictionary *yourDict=[[NSMutableDictionary alloc] init]; 
    [yourDict setObject:[item objectForKey:@"Int_Adr"] forKey:@"Int_Adr"]; 
    [yourDict setObject:[item objectForKey:@"DispName"] forKey:@"DispName"]; 

    [self.recipients addObject:yourDict]; 
} 

//Access 
for (NSDictionary *item in self.recipients) { 
    NSString *address = [item objectForKey:@"Int_Adr"]; 
    NSString *displayName = [item objectForKey:@"DispName"] 
} 
+0

嘿馬爾科姆,我設法得到它的工作感謝您的答案。 :) – Isuru 2013-04-22 09:34:26

+0

很高興聽到這一點 – 2013-04-22 11:54:40

0

原因是每當您更新字典中的值時,爲什麼最後一個值只會到達。爲了實現您的需求,可以爲不同的值使用不同的鍵或將新字典添加到另一個數組中。

0

字典包含鍵和值。鍵是唯一的,如果你這樣做,這意味着:

my_dict["a"] = "hello"; 
my_dict["b"] = "goodbye"; 

字典的全部是:

"a" => "hello" 
"b" => "goodbye" 

然後,如果你這樣做:

my_dict["a"] = "red"; 
my_dict["b"] = "blue"; 

的全部字典是:

"a" => "red" 
"b" => "blue" 

換句話說,與「a」鍵對應的值被「red」覆蓋。

0

它非常簡單見下文

for (NSDictionary *item in contactsArray) { 
    NSMutableDictionary *newDic=[[NSMutableDictionary alloc] init]; 
    [newDic setObject:[item objectForKey:@"Int_Adr"] forKey:@"Int_Adr"]; 
    [newDic setObject:[item objectForKey:@"DispName"] forKey:@"DispName"]; 

    [self.recipients addObject:newDic forKey:@"data"]; 
} 

您需要初始化新的數據集,以包含特定的數據,在你的代碼,你剛纔更新和結束時,最後一個被更新,你只能得到最後一個。

0

要將NSMutableArray轉換爲NSMutableDictionary,需要爲每個NSMutableArray對象提供一個鍵,如下所示。

- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array 
{ 
    id objectInstance; 
    NSUInteger indexKey = 0; 

    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; 
    for (objectInstance in array) 
     [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]]; 

    return (NSDictionary *)mutableDictionary; 
} 

這裏把你的數組作爲參數在這個indexKeyedDictionaryFromArray:方法中。它會給你一個字典。

希望它可以幫助你。

相關問題