2016-05-07 10 views
-1

私はこのようなjsonオブジェクトを持っています。これはURLから取り出されたjson apiであり、私はそれをコアデータに保存しようとしています。コアデータにjsonデータを保存するiOS

{"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); 
    } 
} 

私の質問は、コアデータにjson配列を保存するための保存メソッドの書き方です。

+0

配列名はどのようにjson値を変更するのですか? –

+0

私はコードを更新しました。それを確認してください –

+0

私は答えを投稿します –

答えて

1

まずあなたがAppDelegateからのすべてのメソッドを使用した場合、その後のViewControllerは二つの方法

の下に追加コアデータを使用してすべてのメソッドを追加し、以下のことを指す、

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

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]; 
    } 

//アップロード

- (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メソッドを使用していない、NSManagedObjectクラスです:

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

が最終的にブール上記の呼び出しメソッドは、そのコアのデータ値を保存します。

+0

しかし、1つの問題があります。私はすでにプロジェクトを作成しており、コアデータは使用していません。 (私はプロジェクトを作成している間にコアデータカラムをチェックしていません)何をすればいいですか? –

+0

リンクを参照して、アップロードクラスの変数名を変更してください –

+0

問題はありません。コアデータを追加するには、projectName - >ファイル - >新規 - >コアデータを選択 - >データモデルを選択し、次にコアデータ –

関連する問題