Parse Nsmutabledictionary and extract value

173 views Asked by At

Hello everyone i have one problem in parsing a dictionary here is dictionary

    {
    "@id" = 10;
    activityLocation = "";
    activityPriority = 1;
    actualEndDate = "<null>";
    actualStartDate = "<null>";
    children =         (
    );
    }

i am getting this responce in json and when i tried to parse this dictionay and tried to get the @id value from dictionary it gives me error like

[<__NSCFDictionary 0x7b07d370> valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.

I have tried to get @id value like below code

NSString *id_user = [dictResponce valueForKey:@"@id"];
int user_id = [id_user intValue];

Please suggest me where i am going wrong

2

There are 2 answers

0
Johnykutty On BEST ANSWER

You cannot use valueForKey: method for getting corresponding object from an NSDictionary, it should be NSString *id_user = [dictResponce objectForKey:@"@id"];.

For getting ann object with valueForKey:, it should be an NSMutableDictionary

EDIT:

Actual problem is not the valueForKey: method, The bug is in the key, you cannot use a leading @ in a valueForKey: method, because @ is used for collection operators like @sum, @count etc(its in valueForKeyPath: method, I don't know why its trowing exception in valueForKey: method also) Ref:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html#//apple_ref/doc/uid/20002176-BAJEAIEE

0
johnpatrickmorgan On

You should use objectForKey: not valueForKey: -

NSNumber *id_user = [dictResponce objectForKey:@"@id"];

It looks like the id object is actually an NSNumber not an NSString, so I've also changed that.