Create json string from core data and vice versa?

China☆狼群 提交于 2019-12-10 12:15:34

问题


I have got entities in core data with one to many relationship. How can I convert data in my entities to json string and populate core data entities with json string once it is created?

Any help would be greatly appreciated...


回答1:


for core data to json:

fetch the data from your coredata according to your requirements, & then converts it into json.

visit this link to core data to json also see this one

for json to core data

1)convert JSON to native Cocoa data types (NSDictionary and NSArray)
2)convert NS* into Core Data object

that means you can retrieve your json data into any array or any object & then insert that object or data into core data.

get data from json like this

NSMutableArray *json = (NSMutableArray* )[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
    Book.name = [[[json valueForKey:@"boxesDetail"] objectAtIndex:i] valueForKey:@"name"];
        Book.ID = [NSNumber numberWithInt:[[[[json valueForKey:@"boxesDetail"] objectAtIndex:i] valueForKey:@"number"] intValue]];

now insert this Book data into core data



来源:https://stackoverflow.com/questions/14722129/create-json-string-from-core-data-and-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!