I am receiving below JSON response for saving user preferences in core data.
preferences={
1={
children=({
3=Samsung;4=Nokia;
});id=1;name=Mobiles;
};2={
children=({
5="Samsung Curve TV";
});id=2;name=Electronics;
};
};
Here is my code snippet which is working fine. But I think this is much verbose code.
NSLog(@"Preferences: %@", [response objectForKey:@"preferences"]);
for (NSDictionary *dic in [response objectForKey:@"preferences"]) {
NSLog(@"ID: %@", [[[response objectForKey:@"preferences"] objectForKey:dic] objectForKey:@"id"]);
NSLog(@"NAME: %@", [[[response objectForKey:@"preferences"] objectForKey:dic] objectForKey:@"name"]);
NSLog(@"Children DIC: %@", [[[[[response objectForKey:@"preferences"]
objectForKey:dic] objectForKey:@"children"] objectAtIndex:0] objectForKey:@"3"]);
for (NSDictionary *childDic in [[[[response objectForKey:@"preferences"]
objectForKey:dic] objectForKey:@"children"] objectAtIndex:0]) {
NSLog(@"Child Name: %@", [[[[[response objectForKey:@"preferences"]
objectForKey:dic] objectForKey:@"children"] objectAtIndex:0] objectForKey:childDic]);
}
}
I have 3 questions.
How can I improve my code snippet? is there any shorter way to implement this?
Is this JSON response is good enough for mobile end parsing? is it good JSON format? Is there any JSON response format we should follow as Mobile developers in terms of using Core data (which simply reduces DB implementation as best practice)?
How do I construct a JSON string like this again from Objective-c?