Question

So I have a Json dictionary data coming in like below

{
merchant =     {
    StoreID = 113;
};
token =     {
    Application = Merchant;
    DatabaseID = 113;
    DeviceToken = 9e7737daf0cb1e0572db47f520797adefd0367f66d5d6056f889732dcdb88772;
    ID = 84;
    LoginToken = "00000000-0000-0000-0000-000000000000";
    OS = iOS;
};
}

The data came in before as just the "token" part and I would get the data by using the following code:

[userdefaults setObject:[json objectForKey:@"LoginToken"] forKey:@"loginToken"];

However, the data has been modified into two wrappers and I can't grab the correct data. How can I, for example, get the "storeID" from the new data??

Was it helpful?

Solution

Did you already convert your json-string to a NSDictionary?

NSString *storeID = [(NSDictionary *)[json objectForKey:@"merchant"] objectForKey:@"StoreID"];

If not you need to convert it first:

NSString *json_string = @"{\"merchant\": {\"StoreID\":\"123\"} }";
NSError *error;
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [json_string dataUsingEncoding:NSUTF8StringEncoding]
                                options: NSJSONReadingMutableContainers
                                  error: &error];

NSString storeID = JSON[@"merchant"][@"StoreID"];
NSLog(@"storeID: %@", storeID);

// Log output: storeID: 123

OTHER TIPS

I like better [key] notation: NSString *storeID = json[@"merchant"][@"StoreID"];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top