Fadia Jaradat
Fadia Jaradat

Reputation: 199

How to extract value from dictionary using objective c

I have the below dictionary and I want to extract the date and number_of_movement.

{
    id = 0;
    jsonrpc = "2.0";
    result =     {
        fetalMovements =         {
            "10|22|2014-08-04 00:00:00" =             {
                date =                 {
                    date = "2014-08-04 00:00:00";
                    timezone = "Europe/Prague";
                    "timezone_type" = 3;
                };
                "fetal_movement_id" = 10;
                "number_of_movement" = 3;
                "pregnancy_id" = 22;
            };
            "11|22|2014-08-03 00:00:00" =             {
                date =                 {
                    date = "2014-08-03 00:00:00";
                    timezone = "Europe/Prague";
                    "timezone_type" = 3;
                };
                "fetal_movement_id" = 11;
                "number_of_movement" = 3;
                "pregnancy_id" = 22;
            };
            "12|22|2014-08-03 00:00:00" =             {
                date =                 {
                    date = "2014-08-03 00:00:00";
                    timezone = "Europe/Prague";
                    "timezone_type" = 3;
                };
                "fetal_movement_id" = 12;
                "number_of_movement" = 3;
                "pregnancy_id" = 22;
            };
        };
    };
}

I use this code and I extract result and fetalMovements but I get nil for date and number_of_movement.

NSDictionary *Fetaljson = [NSJSONSerialization JSONObjectWithData:GetMovURL options:kNilOptions error:&error];
NSDictionary *fetalmovDic = [Fetaljson objectForKey:@"result"];
NSDictionary *MovmenteDic= [fetalmovDic objectForKey:@"fetalMovements"];
NSDictionary *DAteDic = [MovmenteDic objectForKey:@"date"]; 
NSMutableArray *array = [DAteDic valueForKey:@"number_of_movement"];

Upvotes: 1

Views: 4621

Answers (1)

Szu
Szu

Reputation: 2252

The code you need (Without NSString to NSDate pasing):

NSDictionary *Fetaljson = [NSJSONSerialization JSONObjectWithData:GetMovURL options:kNilOptions error:&error];
NSDictionary *dict = [Fetaljson valueForKeyPath:@"result.fetalMovements"];
for (id key in [dict allKeys]) {
    NSString *numberOfMovement = dict[key][@"number_of_movement"];
    NSLog(@"Your pair: %@, %@", key, numberOfMovement);
}

Upvotes: 2

Related Questions