could you help me and tell me why my entity is empty ?
i’m populating my entity with my JSON, my entity’s attributes has the same name as in my JSON
this is my code to populate my entity :
NSManagedObjectContext *cxt = [self managedObjectContext];
NSManagedObject *newBoxes = [NSEntityDescription insertNewObjectForEntityForName:@"Boxes" inManagedObjectContext:cxt];
NSDictionary *parsedFeed = [[newBoxes entity] attributesByName];
for (NSString *key in parsedFeed) {
id value = [parsedFeed objectForKey:key];
// Don't assign NSNull, it will break assignments to NSString, etc.
if (value && [value isKindOfClass:[NSNull class]])
value = nil;
@try {
[Boxes setValue:value forKey:key];
} @catch (NSException *exception) {
// Exception means such attribute is not defined in the class or some other error.
}
}
NSError *err;
if (![cxt save:&err]) {
NSLog(@"An error has occured: %@", [err localizedDescription]);
}
NSLog(@"ENTITY %@", newBoxes);
this is the result of my NSLOG :
2012-04-30 11:16:23.352 Wonder[9987:fb03] ENTITY <Boxes: 0x6d87330> (entity: Boxes; id: 0x6d8b010 <x-coredata://EFDCC0BA-644D-42AC-8DE8-452F02B7C680/Boxes/p26> ; data: {
codeArticle = nil;
descriptionBox = nil;
dlu = 0;
kindBox = nil;
nameBox = nil;
nbActivities = 0;
note = 0;
priceBox = 0;
texteMarketing = nil;
typeBox = nil;
})
this is my JSON :
{
"totalBox":{
"boxes":[
{
"codeArticle": "WPCDE01C412L",
"nameBox": "boxName",
"texteMarketing": "boxTextMarketing",
"descriptionBox" : "boxDescritpion",
"nbActivities": 1650,
"kindBox": "boxKind",
"typeBox": "boxType",
"priceBox": 20,
"dlu": 2014,
"note": 3
},
{
"codeArticle": "BOOYAKA!!",
"nameBox": "boxNameName",
"texteMarketing": "boxTextMarketing",
"descriptionBox" : "boxDescritpion",
"nbActivities": 1650,
"kindBox": "boxKind",
"typeBox": "boxType",
"priceBox": 39,
"dlu": 2014,
"note": 3
}
]
}
}
this is my entity :

EDIT : i give my JSON, so i should “tell” my coredata what it should read to populate my entity, right ?
dataToDisplay = [[NSMutableArray alloc] init];
//récupération du chemin vers le fichier contenant le JSON
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"JSON" ofType:@"txt"];
//création d'un string avec le contenu du JSON
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
//Parsage du JSON à l'aide du framework importé
NSDictionary *json = [myJSON JSONValue];
//récupération du total des Boxes
NSDictionary *resultats = [json objectForKey:@"totalBox"];
In the code above you are trying to set values on a non object.
Should be
I would recommend that you take a look at http://rentzsch.github.com/mogenerator/ and this super useful article on JSON and CoreData http://www.cimgf.com/2012/01/11/handling-incoming-json-redux/
Cheers
mbogh