When I NSLog a dictionary output is this:
{
accounts = (
{
"account_number" = 9000012;
"account_type" = "Saver Account";
ato = 0;
balance = "0.0";
},
{
"account_number" = 9000010;
"account_type" = "Primary Account";
ato = 0;
balance = "100000.0";
}
);
}
Now I need same dictionary inside another program (Mocking in my tests). How can I assign this value from copy pasting from console to a NSDictionary?
I tried to make it as NSString as this answer explain
NSString *jsonStr = @" { ({\"account_number\" = 9000012; \"account_type\" = \"Saver Account\"; ato = 0; balance = \"0.0\";});}";
NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
return dict;
Converting NSString to NSDictionary / JSON
but this is returning null.
Again I replace = with : and tried
NSString *jsonStr = @" { ({\"account_number\" : 9000012; \"account_type\" : \"Saver Account\"; ato : 0; balance : \"0.0\";});}";
But still not working?
UPDATED
I managed to convert in this way:
NSDictionary *account = @{ @"account_type":@"Account Type XXX",
@"account_number":@"Account Number 123" };
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
[mainDict setValue:@[account] forKey:@"accounts"];
For two arrays:
NSDictionary *account1 = @{ @"account_type":@"Account Type XXX",
@"account_number":@"Account Number 123" };
NSDictionary *account2 = @{ @"account_type":@"Account Type YYY",
@"account_number":@"Account Number 456" };
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
[mainDict setValue:@[account1,account2] forKey:@"accounts"];
But @Duncan C claimed this way is not safe!