0

I want to retrieve data from Array in iOS sent from PHP webservice. Following is the structure of the Array:

[
    {
        "fullname": "Kate Bell",
        "email": "kate-bell@mac.com"
    },
    {
        "fullname": "Kate Bell",
        "email": "www.creative-consulting-inc.com"
    },
    {
        "fullname": "Daniel Higgins",
        "email": "d-higgins@mac.com"
    },
    {
        "fullname": "John Appleseed",
        "email": "John-Appleseed@mac.com"
    }
]

Note: Array can be of any length.

PHP code:

mysql_select_db("mydb");

    $ReturningArray = array();

    foreach($Contacts_Array as $arr)
    {
        foreach($arr['emails'] as $email_address)
        {
            $query = "select email from table where email='".$email_address."'";
            $result = mysql_query($query);

            $row = mysql_fetch_assoc($result);

            if(empty($row))
            {
                $Contact = array(
                "fullname" => $arr['fullname'],
                 "email"   => $email_address
                 );

                $ReturningArray[] =  $Contact;
            }
        }

    }

    echo json_encode($ReturningArray);

Update:

I have tried this code but it doesn't work

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        // NSString *Result = [[NSString alloc] initWithData:data      encoding:NSUTF8StringEncoding];
   // NSLog(@"Result in NonTroopeUsers : %@",Result);

        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        NSLog(@"COUNT :%d",[array count]);

        for (NSDictionary* item in array)
        {
            NSString *fullname = [item objectForKey:@"fullname"];
            NSString *email = [item objectForKey:@"email"];

            NSLog(@"Full name: %@",fullname);

            NSLog(@"email: %@",email);
        }

    }

Array count is Zero but the commented code shows data recieved in nslog.

Ali Shahid
  • 516
  • 1
  • 5
  • 21

3 Answers3

0

Do not perform the conversion in connection: didReceiveData: method. We might have only a partial data. And this may get called many times to giving chunks of datas. You have to add these chunks of partial datas to get the full Data.

You can use NSJSONSerialization for parsing the JSON.

NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

Also,

Create an memberVariable

 NSMutableData *netData;

Implement the following methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
     if(netData == nil)
          netData = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [netData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
      //// Here convert the Full Data to JSON, 
     NSArray *array = [NSJSONSerialization JSONObjectWithData:netData options:kNilOptions error:nil];
     NSLog(@"%@",array);


}
IronMan
  • 1,505
  • 1
  • 12
  • 17
0

well

id responseArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(!error){
  if([responseArray isKindOfClass:[NSArray class]])
     for(id entry in responseArray){
        if([entry isKindOfClass:[NSDictionary class]]){
         // do what you want with the dictionary
       }
     }
   }
 }
oiledCode
  • 8,589
  • 6
  • 43
  • 59
0

I have solved the problem. I was using var_dump($SomeVariable) at the start of php file. When I removed that I am able to retrieve data.

Ali Shahid
  • 516
  • 1
  • 5
  • 21