0

A web service call returns a JSON response and I'm putting it in to an NSMutableArray. The JSON response looks like this,

 (
        {
        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;
    }
)

Now I want to create another NSMutableDictionary containing only the keys, DispName and Int_Adr.

So I'm doing something like this. I have declared a NSMutableDictionary in the .h file like this.

@property (nonatomic, strong) NSMutableDictionary *recipients;

In the .m file,

// 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!

I'm setting only those 2 values to the recipients dictionary. But when I log the output, it shows only the last set.

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

How can I solve this problem?

Thank you.

5 Answers 5

2

This is my suggestion to you, use NSMutableArray together with NSMutableDictionary. The JSON indicates it is array of dictionary. You have to redesign your code a bit.

@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"]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Malcolm, I managed to get it working thanks to your answer. :)
0

reason was every time you are updating the values in dictionary that's why last values only coming.for achieving your requirement take different keys for different values or add new dictionary to another array.

Comments

0

A dictionary has keys and values. The keys are unique, which means if you do this:

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

The entirety of the dictionary is:

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

Then if you do this:

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

The entirety of the dictionary is:

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

In other words, the value corresponding to the "a" key was overwritten with "red".

Comments

0

Its very simple see below

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"];
}

You need to init new set of data to contain that particular data, in your code, you were just updating the and in the end, the last one gets updated, and you get the last one only.

Comments

0

For converting NSMutableArray to NSMutableDictionary you need to provide a key to each object of NSMutableArray as below.

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

Here pas your array as parameter in this indexKeyedDictionaryFromArray: method. It will return you a dictionary.

Hope it helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.