【发布时间】:2014-11-28 07:01:06
【问题描述】:
我正在制作一些应用程序,该应用程序调用 PHP 来传递 JSON 文件。
在我的 PHP 代码中,我有这样的 JSON 结果{"files":[{"name":"sharing-config.txt","date":"28-11-2014-11-59-59"},{"name":"2.jpg","date":"28-11-2014-14-25-35"}]}
现在我想将 JSON 结果调用到我的 objc 函数并将其返回为 NSArray,有人可以帮助我吗?
非常感谢您的帮助:)
对不起,我的英语不好。
更新
下面是我的 PHP 代码
$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);
我的 ObjC 代码
-(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);
}
这是我从 xCode 得到的日志
2014-11-28 14:35:51.905 balisfinestvillas[17577:9635538] This4: {"files":[{"name":"sharingconfig.txt","date":"28112014115959"},{"name":"2.jpg","date":"28112014142535"}]}
干杯:)
【问题讨论】:
-
请显示您的代码...
-
请看我的更新,谢谢
标签: php ios objective-c json