如何将JSON对象保存到Core Data? [英] How can i save JSON objects to Core Data?

查看:135
本文介绍了如何将JSON对象保存到Core Data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是核心数据中的一个nwebie,我设计了一个基于导航的应用程序,我使用的一些数据是在运行时创建的(来自通过JSON的URL)。我花了几个教程搜索了将近一天,但还没有意识到如何将传入的JSON数据保存到我的Core Data模型中的Entity(或事件?)。我获取 DetailViewController 类中的数据,我需要将这些数据保存到Core Data(我已经准备了一个具有7个属性的实体)。任何人都可以帮忙吗?(如果你知道一个很好的教程或示例代码我会很高兴)

I'm a nwebie in Core Data, i have designed a navigation based application and some of the data i use are created on run time(come from a URL via JSON). I took a few tutorials an searched for almost a day but haven't still realized how to save the incoming JSON data to the Entity (or event?) in my Core Data model. I fetch the data in the DetailViewController class and i need to save this data to Core Data(I have prepared an Entity with 7 properties). Can anyone please help?(If you know a good tutorial or sample code i will be pleased)

EDIT 这可能有点具体,我真的有麻烦,需要只是一点点帮助。
我的数据来自一个restful服务器(我在PHP中写的),首先用户输入他/她的登录信息(我已保存到服务器上的数据库),当响应数据来时我将在不同的视图中使用不同的元素(例如 user_id 将用于视图, buttonData 等等)。我的问题是,我将如何保存JSON数据到我的核心数据模型(现在有树实体)。提前感谢

EDIT This may be a little specific but i really have trouble with and need just a little help. My data comes to the app from a kind of restful server(i wrote it in PHP), firstly user enters his/her login informations(which i have saved to the database on server before) and when the response data comes i will use different elements of it in differen views(for example the user_id will be used on a view and the buttonData etc on other views). My question is, how will i save JSON data into my core data model(has tree Entities for the moment). Thanks in advance

注意:我已经安装了很多,但是找不到任何关于像我这样的应用程序的回答和教程

Note: I lokked arround a lot but couldn't find any answer&tutorial about an app like mine

推荐答案

最好的方法是创建与JSON结构相对应的实体。最简单的是每个JSON对象成为一个实体,数组变成实体数组。但是,合理的,并且不要对基本上是其超级对象的一部分的JSON子对象引入过多的过分。

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.

当你创建实体时,你可以开始解析和翻译。使用一些JSON框架(从iOS5开始有一个来自苹果)和解析JSON字符串到对象树,其中根项目是一个NSArray或NSDictionary,子元素将NSArray,NSDictionary,NSNumber,NSString或NSNull。

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.

在迭代循环中逐个遍历它们,并根据您的核心数据实体属性分配值。您可以在这里使用NSKeyValueCoding,并避免过多的手动映射属性名称。如果您的JSON属性与实体属性的名称相同,您可以仅浏览所有字典元素,并将其解析为同名的属性。

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.

在类似情况下,我的解析代码如下:

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:


  • 使用某些自定义映射,以防您想要以不同的方式放置JSON值

  • 如果您的JSON包含子对象或子对象数组,您将需要检测这些情况,例如在setter中,并启动新的解析一级。否则,在我的示例中,您将面临将NSDictionary对象分配给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.

这篇关于如何将JSON对象保存到Core Data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆