-2

i am geting data in a nsmutabledictionay from api and i need to add this data into tableview cell, so i need to convert it into nsmutable array. but when tried to add data from dictonary to aarray it picks only last object of dictonary. pleae look at my code and response.

i need to parse stop1 and stop2 address 

 NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *task = [session dataTaskWithRequest:theRequest
                                        completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {

                                  NSLog(@"Response:%@ %@\n", response, error);
                                  if(error == nil)
                                  {

                                      NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                      NSLog(@"Data = %@",text);
                                  }

                                  dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];
                                  NSMutableDictionary *temp = [NSMutableDictionary new];

                                  NSArray *IDArray = [dictionary objectForKey:@"docs"];
                                  for (NSDictionary *Dict in IDArray)
                                  {
                                      [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                                      NSLog(@"id %@", temp);


                                  }


                                  [getid addObject:temp];
                                  NSLog(@"getid details %@", getid);


                                  if (getid)
                                  {
                                      [_ScheduleRidetable reloadData];
                                  }



                              }];

this is my response which i get in console

   id {
"_id" = 573c1beb7051509c0ec94936;
     }
 2016-05-18 14:41:48.641 GGR Driver Staging[11546:6724511] id {
"_id" = 573c17f8261b2bc76e228721;
    }
  2016-05-18 14:41:48.642 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0f20261b2bc76e22871e;
     }
 2016-05-18 14:41:48.642 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0eb4261b2bc76e22871d;
  }
 2016-05-18 14:41:48.643 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0e15261b2bc76e22871c;
    }
   2016-05-18 14:41:48.644 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0d0c261b2bc76e22871b;
     }
  2016-05-18 14:41:48.645 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0c5f261b2bc76e22871a;
  }
  2016-05-18 14:41:48.646 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0acf261b2bc76e228717;
   }
2016-05-18 14:41:48.647 GGR Driver Staging[11546:6724511] getid details (
    {
    "_id" = 573c0acf261b2bc76e228717;
     }
  )


{"message":"success","data":{"docs":[{"_id":"573c1beb7051509c0ec94936","user":{"phone":"9574863210","image":"","name":"user12"},"distance":45123,"bookingNumber":"429r2ckh8pa8","bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":103.99,"estimatedDuration":"4557","created":"2016-05-18T07:38:12.037Z","stop2":{"address":"New Delhi, New Delhi 110001, India","location":[28.6139391,77.2090212]},"stop1":{"address":"Sector 18, Noida, Uttar Pradesh 201301, India","location":[28.570317000000003,77.3218196]},"destination":{"address":"Gurgaon, Haryana 122001, India","location":[28.459496499999997,77.0266383]},"currentLocation":{"address":"A20A Block, Sector 58","location":[28.6024597,77.3609439]}},{"_id":"573c17f8261b2bc76e228721","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0f20261b2bc76e22871e","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0eb4261b2bc76e22871d","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":9913,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":31.71,"estimatedDuration":"1533","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida, Uttar Pradesh, India","location":[28.5355161,77.39102649999995]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0e15261b2bc76e22871c","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0d0c261b2bc76e22871b","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0c5f261b2bc76e22871a","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0acf261b2bc76e228717","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}}],"total":10,"limit":8,"page":":1","pages":2}}

This is my api form there i get response.

NSURL *theURL = [NSURL URLWithString:@"http://qa.networc.in:1336/api/dispatcher/rideScheduled/:1"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL      cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];

//Specify method of request(Get or Post)
[theRequest setHTTPMethod:@"GET"];

//Pass some default parameter(like content-type etc.)
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

[theRequest addValue:token forHTTPHeaderField:@"x-access-token"];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
if (responseData == nil)
{

    return;
}

NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&theError];
NSLog(@"url to send request= %@",theURL);
NSLog(@"%@",dataDictionaryResponse);
sandeep tomar
  • 303
  • 1
  • 5
  • 17

5 Answers5

1

do like

                 getid = [NSMutableArray New];

                    for (NSDictionary *Dict in IDArray)
                              {
                                   NSMutableDictionary *temp = [NSMutableDictionary new];
                                  [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                                 // for fetch stop1 and stop2 use like
                                   NSMutableDictionary *stp1 = [Dict objectForKey:@"stop1"] 
                               if ([[stp1 allKeys] containsObject:@"address"]) {
// key exists.
                         [temp setObject:[stp1 objectForKey:@"address"] forKey:@"address"];
                            }
               }


                                NSMutableDictionary *stp2 = [Dict objectForKey:@"stop2"] ;

                         if ([[stp2 allKeys] containsObject:@"address"]) {
// key exists.
                           [temp setObject:[stp2 objectForKey:@"address"] forKey:@"address1"];
                            }

                                 [getid addObject:temp];
                              }

                             if (getid.count>0)
                              {
                                  [_ScheduleRidetable reloadData];
                              }

Step-2

on retrieve in tableview

 idname.text  = [getid objectAtIndex:objectAtIndex:indexPath.row]objectForKey:@"_id"];
  stop2.text  = [getid objectAtIndex:objectAtIndex:indexPath.row]objectForKey:@"address1"];
    stop1.text  = [getid objectAtIndex:objectAtIndex:indexPath.row]objectForKey:@"address"];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

You are setting value for same key in for loop [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"]; this statement every time override the value of _id so when you add it to getid array it have only last _id with it's value.

so now, if you want array of dictionary then you can simply put your [getid addObject:temp]; statement in for loop (at last)

If you want array of it value (array of string), here string means your id like 573c0acf261b2bc76e228717 then you can do something like,

in your for loop,

  [getid addObject:[Dict objectForKey:@"_id"] forKey:@"_id"];

So this will add only ids in getid array. your array will looks someting like : [573c17f8261b2bc76e228721, 573c0f20261b2bc76e22871e, 573c0eb4261b2bc76e22871d, etc to last id]

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • yes sir , i tried to make it and its work great but, how will i add those string in my tableview cell, ia m using prototype cells – sandeep tomar May 18 '16 at 09:40
  • For that you need to implement `cellforrowatindexpath` delegate method and from that you can fetch value from array using `indexpath.row` considering as index. for exxample if you want to show id in cell label then, `cell.labelId.text = [getid objectAtIndex : indexpath.row];` like wise – Ketan Parmar May 18 '16 at 09:43
0
   Array *IDArray = [dictionary objectForKey:@"docs"];
             for (NSDictionary *Dict in IDArray)
            {
               NSMutableDictionary *temp = [NSMutableDictionary new];
               [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                [getid addObject:temp];

             }
Prabhat
  • 96
  • 7
  • sir if i use this method than i get response in string but i need to display it on cell by the help of objectforkey – sandeep tomar May 18 '16 at 09:32
  • Nsdictionary *dict = getid[indexpath.row]; than cell.textLable.text = [dict objectForkey:"id"]; in cellForRowAtIndexPath – Prabhat May 18 '16 at 09:36
0

because NSMUtableDictionary has only one key,you can

for(int i=0,i< IDArray.count,i++){
    [temp setObject:[Dict objectForKey:@"_id"] forKey:[NSString stringformationWith @"_id%d",i]];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
RGB_DS
  • 98
  • 4
0
Use this code,

NSMutableArray *tempArray=[[NSMutableArray alloc]init];

for (NSDictionary *Dict in IDArray)
{
    [tempArray setValue:[Dict objectForKey:@"_id"] forKey:@"id"];
};

[allValues addObjectsFromArray:tempArray];

allValues is also NSMutableArray,

hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30