-3

I'm making some app, where the app call PHP to pass the JSON file.

In my PHP code I have a result of JSON like this {"files":[{"name":"sharing-config.txt","date":"28-11-2014-11-59-59"},{"name":"2.jpg","date":"28-11-2014-14-25-35"}]}

Now I want call the JSON result to my objc function and return it as NSArray, is anybody can help me?

Many thank for your help :)

Sorry for my bad English.

UPDATE

Bellow is my PHP Code

$directory  = '../download/sponsors';

$arrayFiles     = array();

if ($handle = opendir($directory)) {


$i = 1 ;

while (false !== ($entry = readdir($handle))) {

    if($entry != "." && $entry != ".."){

        if (file_exists($directory  . "/" .$entry)) {

            $fileList       = array("name" => $entry, "date" => date ("d-m-Y-H-i-s", filemtime($directory . "/" .$entry)));
            $arrayFiles[]   = $fileList;

        }

    }
}

closedir($handle);
}


$obj = new stdClass();
$obj->files = $arrayFiles;

echo json_encode($obj);

My ObjC code

-(void)getPHPdata {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://appserver.com/applications/appUpdate.php"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    receivedData = [[NSMutableData data] retain];
    NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"This1: %@", receivedData);
    NSLog(@"This2: %@", receivedDataString);
} else {
    // Inform the user that the connection failed.
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0];
NSLog(@"This3: %@", receivedData);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];

filePath = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; // Get the return value from server

connectionSuccess = YES;


NSLog(@"This4: %@", [filePath stringByReplacingOccurrencesOfString:@"-" withString:@""]);
NSLog(@"This5: %@", receivedData);
}

Here is the LOG i got from xCode

2014-11-28 14:35:51.905 balisfinestvillas[17577:9635538] This4: {"files":[{"name":"sharingconfig.txt","date":"28112014115959"},{"name":"2.jpg","date":"28112014142535"}]}

Cheers :)

ilovebali
  • 513
  • 2
  • 7
  • 20

1 Answers1

2

your JSon response is started with NSDictionary not the NSArray

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

 NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:receivedData options: kNilOptions error:nil];

  NSMutableArray *finalresult=[[NSMutableArray alloc] init];  // allocating the array

    NSArray *responseArr=[jsonDict objectForKey@"files"];

    for (NSDictionary *tmp in responseArr) {

      NSMutableDictionary  *itemshowdetails=[[NSMutableDictionary alloc]init];

        [itemshowdetails setValue:[tmp objectForKey:@"name"] forKey:@"name"];
        [itemshowdetails setValue:[tmp objectForKey:@"date"] forKey:@"date"];

         [finalresult addObject:itemshowdetails];

    }

   }

retrieve

this method is used for where u want to access your data , customize your self

 for (int i=0;i<[finalresult count];i++)
{
 NSLog(@"%@",[[finalresult objectAtIndex:i] objectForKey:@"name"];
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143