0

In my application I am using a JsonModel for parsing JSON response from server and while storing it in Core Data I am using NSManagedObject and NSManagedContext which is provided by Apple itself. Now whenever I fetch I want to convert the NSManagedObject to JsonModel. Now the problem is I have to use two separate class to manage jsonModel and NSManagedObject.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dharni
  • 1

2 Answers2

0

Hope it helps you. first try to convert your NSMangedObject to NSDictionary.

NSArray *keys = [[[yourObject entity] attributesByName] allKeys];
NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];

the you have to use dict as a JSON or convert them into JSON string if needed.

Vvk
  • 4,031
  • 29
  • 51
0

You can get help from this link.

Get Core Data objects to JSON

Or in easy way from andrew-madsen's Answer

NSManagedObject *managedObject = ...;
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", ..., nil]; // These are the keys for the properties of your managed object that you want in the JSON
NSString *json = [[managedObject dictionaryWithValuesForKeys:keys] JSONRepresentation];

For more detail try this link

nsmanagedobject-to-json

Community
  • 1
  • 1
Badal Shah
  • 7,541
  • 2
  • 30
  • 65