从核心数据创建json字符串,反之亦然? [英] Create json string from core data and vice versa?
问题描述
我在核心数据中具有一对多关系的实体.创建实体后,如何将实体中的数据转换为json字符串并用json字符串填充核心数据实体?
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...
推荐答案
将核心数据转换为json:
for core data to json:
根据您的要求从您的核心数据中获取数据,然后将其转换为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
将json转换为核心数据
for json to core data
1)将JSON转换为本地Cocoa数据类型(NSDictionary和NSArray)
2)将NS *转换为Core Data对象
1)convert JSON to native Cocoa data types (NSDictionary and NSArray)
2)convert NS* into Core Data object
这意味着您可以将json数据检索到任何数组或任何对象中&然后将该对象或数据插入核心数据.
that means you can retrieve your json data into any array or any object & then insert that object or data into core data.
像这样从json获取数据
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]];
现在将此Book数据插入核心数据
now insert this Book data into core data
这篇关于从核心数据创建json字符串,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!