0

Web サービス呼び出しは JSON 応答を返し、それを NSMutableArray に入れています。JSON レスポンスは次のようになります。

 (
        {
        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のみを含む別のものを作成したいと思います。

だから私はこのようなことをしています。NSMutableDictionaryこのように .h ファイルでa を宣言しました。

@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;
}

どうすればこの問題を解決できますか?

ありがとうございました。

4

5 に答える 5

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"]
}
于 2013-04-22T08:07:06.187 に答える
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」で上書きされました。

于 2013-04-22T07:53:08.720 に答える
0

理由は、辞書の値を更新するたびに、最後の値だけが来る理由です。要件を達成するために、異なる値に対して異なるキーを取得するか、新しい辞書を別の配列に追加します。

于 2013-04-22T07:49:40.343 に答える
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"];
}

その特定のデータを含めるには、新しいデータセットを初期化する必要があります。コードでは、を更新するだけで、最終的に最後のデータが更新され、最後のデータのみが取得されます。

于 2013-04-22T07:59:36.940 に答える