在核心数据iOS中保存json数据

时间:2016-05-07 06:19:05

标签: ios arrays json core-data

我有一个看起来像这样的json对象。这是从url获取的json api,我试图将其保存在核心数据中。

{"moods_name":"mood1",
"description":"mood",
"c1":"9BFF80",
"c2":"EA8CFF",
"c3"    :"D1FFB0",
"c4":"FF63FC","c5":"6B6B6B",
"font_name":"Default",
"font_size":"3",
"font_color":"000000"}

如何按字符串存储此字符串,或者是否存在另一种存储方法。请帮帮我。

这是我获取json数组的方法。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSError * localError=nil;
    NSArray *listArray = [NSJSONSerialization  JSONObjectWithData:getData options:0 error:&localError];
    if (!listArray) {
       NSLog(@"error parsing json %@",localError);
    }
    else{
       NSLog(@" JSON DATA%@",listArray);
   }
}

我的问题是如何编写save方法来保存核心数据中的json数组。

2 个答案:

答案 0 :(得分:1)

首先使用核心数据添加所有方法,并在下面参考,

http://code.tutsplus.com/tutorials/core-data-from-scratch-core-data-stack--cms-20926 http://www.techotopia.com/index.php/An_iOS_7_Core_Data_Tutorial

如果您使用AppDelegate中的所有方法,那么ViewController将在下面添加两个方法

如果你得到listArray值,那么使用下面的代码,

ViewController方法:

   - (IBAction)savevalues:(id)sender {
         // first convert the json array to NSString type code below,

         NSString *m_name = [listArray valueForKey:@"moods_name"];
         NSString *desc = [listArray valueForKey:@"description"];
         NSString *c1 = [NSString stringWithFormat:@"%@",[listArray valueForKey:@"c1"]];
         NSString *c2 = [NSString stringWithFormat:@"%@",[listArray valueForKey:@"c2"]];
         NSString *c3 = [NSString stringWithFormat:@"%@",[listArray valueForKey:@"c3"]];
         NSString *c4 = [NSString stringWithFormat:@"%@",[listArray valueForKey:@"c4"]];
         NSString *font_name = [listArray valueForKey:@"font_name"];
         NSString *font_size = [NSString stringWithFormat:@"%@",[listArray valueForKey:@"font_size"]];
         NSString *font_color = [NSString stringWithFormat:@"%@",[listArray valueForKey:@"font_color"]];

         [self createNewListWithTitle:m_name describtion:desc c1Val:c1 c2Val:c2 c3Val:c3 c4Val:c4 font_nameVal:font_name font_sizeVal:font_size font_colorVal:font_color];
    }

// Upload是NSManagedObject类,

- (BOOL) createNewListWithTitle:(NSString *)paramTitle describtion:(NSString *)paramDesc c1Val:(NSString *)paramC1 c2Val:(NSString *)paramC2 c3Val:(NSString *)paramC3 c4Val:(NSString *)paramC4 font_nameVal:(NSString *)paramfont_name font_sizeVal:(NSString *)paramfont_size font_colorVal:(NSString *)paramfont_color {

    BOOL success = NO;
    if ([paramTitle length] == 0){
        NSLog(@"Title is mandatory.");
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"List Not Created" message:@"Please Enter the Keyword, Choose Color" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert1 show];

        return NO;
    }

    Upload *newList = [NSEntityDescription
                       insertNewObjectForEntityForName:@"Upload"
                       inManagedObjectContext:self.managedObjectContext];
    if (newList == nil){
        NSLog(@"Failed to create the new List");

        return NO;
    }



    newList.name = paramTitle;

    newList.desc =paramDesc;

    newList.c1 =paramC1;

    newList.c2 =paramC2;

    newList.c3 =paramC3;

    newList.c4 =paramC4;


    newList.fontName=paramfont_name;

    newList.fontSize=paramfont_size;

    newList.fontColor=paramfont_color; 

    NSError *savingError = nil;

    if ([self.managedObjectContext save:&savingError]){
        NSLog(@"New List was created");
        return YES;
    }
    else {
        NSLog(@"Failed to save the new List, Error = %@", savingError);
    }

    return success;
}

请勿忘记#import "Upload.h"并使用viewDidLoad方法:

id delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = [delegate managedObjectContext];

最后调用上面的Bool方法,它保存核心数据值。

答案 1 :(得分:0)

请参阅此网站,它对您有所帮助。

http://www.appcoda.com/core-data-tutorial-update-delete/