I write JSON to my file :
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dictionary error:&error];
[jsonData writeToURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@",
NSTemporaryDirectory(), @"file.json"]] atomically:YES];
But how do i read it back?
I tried load the file into NSData, and convert to a NSDictionary like so
// uses toll-free bridging for data into CFDataRef and CFPropertyList into NSDictionary
CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data,
kCFPropertyListImmutable,
NULL);
// we check if it is the correct type and only return it if it is
if ([(id)plist isKindOfClass:[NSDictionary class]])
{
return [(NSDictionary *)plist autorelease];
}
else
{
// clean up ref
CFRelease(plist);
return nil;
}
But it crashed because i think NSData is not a plist.
Has CJSONSerializer got a method to extract the data from the file and help me convert it to a dictionary?
Thanks,
-Code
You use a serializer to convert an object to JSON, and deserializer for the opposite direction. And indeed TouchJSON has a CJSONDeserializer class: