-3

I am parsing data using ASIHTTPRequest. Now, I want to store that data in core data. Used this API. My Entity Name SMS and includes three atrributes Id,Name and IsActive. This code to parse data.

-(void)DataRetireve
{
    deatilinfoarray = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:   
    @"http://sms.instatalkcommunications.com/apireq/GetSMSCategories?
    t=1&h=admin&param=1"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];``
    [request setDelegate:self];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{

    NSError *error;

    NSString *responseString = nil;

    if (!error)
    {
        responseString = [request responseString];

        SBJsonParser *parser = [[SBJsonParser alloc] init] ;

        NSMutableArray *jsondata = [parser objectWithString:responseString];
        NSMutableArray *jsondata1 = [[NSMutableArray alloc]init];
       info *myinfo=[[info alloc]init];
        for(int i=0;i<jsondata.count;i++)
        {

            myinfo.Id=@"Id";
            myinfo.Name=@"Name";
            myinfo.IsActive=@"IsActive";

            [jsondata1 addObject:myinfo];
             NSLog(@"%@",responseString);

        }
           for(int i=0;i<jsondata1.count;i++)
           {
            myinfo= [jsondata1 objectAtIndex:i];

                 NSManagedObjectContext *context=[self managedObjectContext];
               NSManagedObject *sms = [NSEntityDescription insertNewObjectForEntityForName:@"SMS" inManagedObjectContext:context];


               NSLog(@"%@", context);

               [sms setValue:@"Id" forKey:@"Id"];
               [sms setValue:@"Name" forKey:@"Name"];
               [sms setValue:@"IsActive" forKey:@"IsActive"];

               [jsondata1 addObject:myinfo];

            }

Done code for that but give me error

Yves M.
  • 29,855
  • 23
  • 108
  • 144

1 Answers1

1

First, I think ASIHTTPRequest is kind of outdated. According to the original site was last modified on 2011. I recommend you to switch to http://afnetworking.com/ .

Now, regarding the JSON to Core Data. First, you should parse the JSON data using NSJSONSerialization. You can take a look at this question (How to use NSJSONSerialization) for more information.

Then, create a new NSManagedObject for your entity:

NSManagedObject *aSMS = [NSEntityDescription insertNewObjectForEntityForName:@"SMS"
                                                      inManagedObjectContext:context];

And then just set the values. Like:

aSMS.Name = JSON[@"Name"];
Community
  • 1
  • 1
LuisEspinoza
  • 8,508
  • 6
  • 35
  • 57