将JSON数据转换和存储到iOS核心数据中。

时间:2022-06-27 16:28:11

Basically I'm working on an app to allow users to track their tv shows. A user can click their tv show to get a season and episode breakdown.

基本上,我正在开发一款应用程序,允许用户跟踪他们的电视节目。用户可以点击他们的电视节目来获得一个季节和片段的崩溃。

To achieve this, I am trying to gather JSON data from this API, and then store the data into core data. The API call is this: http://api.trakt.tv/show/summary.json/36590b30dc7d0db9ebd3153b1a989e5d/arrow/1

为了实现这一点,我尝试从这个API收集JSON数据,然后将数据存储到core data中。API调用是:http://api.trakt.tv/show/summary.json/36590b30dc7d0db9ebd3b1a989e5d/arrow/1。

I can successfully store the values of: title, year, url, first_aired etc. But I can't work out how to store the season and episode information into my core data (located about half way down the JSON API call)

我可以成功地存储:title、year、url、first_等的值,但我无法计算出如何将这个季节和片段信息存储到我的core data中(位于JSON API调用的一半位置)

I have included a link to a screenshot of how I've set out my core data model: !http://i546.photobucket.com/albums/hh427/camcham/ScreenShot2013-10-17at34449AM.png

我已经把我的核心数据模型的屏幕截图包括进来了:http://i546.photobucket.com/albums/hh427/camcham/ScreenShot2013-10-17at34449AM.png。

The code below is how I'm currently trying to store the JSON data into my core data (using MagicalRecords)

下面的代码是如何将JSON数据存储到我的core data中(使用MagicalRecords)

NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];


        Show *showp = [Show MR_findFirstByAttribute:@"sID" withValue:showID inContext:localContext];
        if (![showp.sID isEqualToString:showID])
        {
            //Create New Show in current thread
            Show *showContext = [Show MR_createInContext:localContext];
            showContext.title = showTitle;
            showContext.poster = showPoster;
            showContext.year = showYear;
            showContext.sID = showID;
            //code above this comment correctly adds right JSON info to core data and i can access and display it properly

The next part of my code I have tried to convert an NSArray to NSSet, as my 'seasons' relationship is of type NSSet, however I believe the JSON data is NSArray. I am getting the following error: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber managedObjectContext]: unrecognized selector sent to instance 0xa22a680'

我的代码的下一部分尝试将NSArray转换为NSSet,因为我的“季节”关系属于NSSet,但是我相信JSON数据是NSArray。我得到了以下错误:*终止应用程序,因为未捕获异常'NSInvalidArgumentException',原因是:'-[__NSCFNumber managedObjectContext]:未识别的选择器发送到实例0xa22a680'

                NSArray *show = [(NSSet *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
            showContext.seasons = [NSSet setWithArray:show];

The code below does not work as intended. episode.title for example, stores every single episode's title, instead of just the single title for a particular episode.

下面的代码不像预期的那样工作。一集。例如,每一集的标题都要存储,而不是某一集的标题。

            Season *season = [Season MR_createInContext:localContext];
            season.seasonNumber = [(NSDictionary *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
            season.episodes = [(NSDictionary *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"episodes"];


            Episode *episode = [Episode MR_createInContext:localContext];
            episode.title = [[(NSDictionary *)[season.episodes objectForKey:@"seasons"] valueForKey:@"episodes"] valueForKey:@"title"];
            episode.overview = [[(NSDictionary *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"episodes"] valueForKey:@"overview"];

So to sum it all up, I would love for someone to demonstrate the correct way to store the tv seasons and episodes from my JSON API, so I can then utilise this data in my app!

总结一下,我希望有人能通过我的JSON API来展示正确的方法来存储电视季和片段,这样我就可以在我的应用程序中使用这些数据了!

1 个解决方案

#1


1  

Removing the cast to (NSSet *) should fix the error.

删除cast到(NSSet *)应该修复错误。

NSArray *show = [[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
showContext.seasons = [NSSet setWithArray:show];

instead of

而不是

NSArray *show = [(NSSet *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
showContext.seasons = [NSSet setWithArray:show];

#1


1  

Removing the cast to (NSSet *) should fix the error.

删除cast到(NSSet *)应该修复错误。

NSArray *show = [[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
showContext.seasons = [NSSet setWithArray:show];

instead of

而不是

NSArray *show = [(NSSet *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
showContext.seasons = [NSSet setWithArray:show];