How can i save JSON objects to Core Data?

左心房为你撑大大i 提交于 2019-12-01 21:57:21

The best way to do that would be to create entities corresponding to JSON structure. Easiest was is when each JSON object becomes an entity, and arrays become arrays of entities. Be reasonable, however, and don't introduce too much overkill for JSON subobjects that are essentially part of its superobject.

When you have created entities, you can start off with the parsing and translation. Use some JSON framework (starting from iOS5 there's one from Apple) and parse JSON string into object tree, where root item is either an NSArray or NSDictionary, and subelements will be NSArray, NSDictionary, NSNumber, NSString or NSNull.

Go over them one by one in iterational loops and assign according values to your core data entity attributes. You can make use of NSKeyValueCoding here and avoid too much manual mapping of the attribute names. If your JSON attributes are of the same name as entity attributes, you'll be able to just go over all dictionary elements and parse them into attributes of the same name.

Example

My parsing code in the similar situation was as follows:

NSDictionary *parsedFeed = /* your way to get a dictionary */;
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 {
        [yourCreatedEntity setValue:value forKey:property];
    } @catch (NSException *exception) {
        // Exception means such attribute is not defined in the class or some other error.
    }
}

This code will work in trivial situation, however, it may need to be expanded, depending on your needs:

  • With some kinds of custom mappings in case you want your JSON value be placed in differently named attribute.
  • If your JSON has sub-objects or arrays of sub-objects, you will need to detect those cases, for example in setters, and initiate new parsing one level deeper. Otherwise with my example you will face the situation that assigns NSDictionary object to an NSManagedObject.

I don't think it is reasonable to dive into these, more advanced matters in scope of this answer, as it will expand it too much.

This lib helps me lot

Features
  • Attribute and relationship mapping to JSON key paths.

  • Value transformation using named NSValueTransformer objects.

  • Object graph preservation.

  • Support for entity inheritance

  • Works vice-versa

I suggest you to use this library : https://github.com/TouchCode/TouchJSON And then if you want to make a factory to parse json and feed your code data, you can use selectors to call methods to fill all your attributes.

Chances are your JSON data gets converted to an NSDictionary or NSArray (or some combination of the two). Simply extract the key/values from the JSON structure and add them to your entity class.

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