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!

我只将这两个值设置为收件人字典。但是当我记录输出时,它只显示最后一组。

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

我怎么解决这个问题?

谢谢你。

4

5 回答 5

2

这是我给你的建议,NSMutableArrayNSMutableDictionary. 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

原因是每次您更新字典中的值时,这就是为什么最后一个值才会出现。为了实现您的要求,为不同的值采用不同的键或将新字典添加到另一个数组。

于 2013-04-22T07:49:40.343 回答
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

它非常简单,见下文

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 回答
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:方法中的参数传递。它会给你一个字典。

希望它可以帮助你。

于 2013-04-22T08:14:58.203 回答