Objective C从PHP获取JSON [重复]
Posted
技术标签:
【中文标题】Objective C从PHP获取JSON [重复]【英文标题】:Objective C get JSON from PHP [duplicate] 【发布时间】: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"]
干杯:)
【问题讨论】:
请显示您的代码... 请看我的更新,谢谢 【参考方案1】:您的JSon
响应以NSDictionary
开头,而不是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];
检索
此方法用于您想要访问您的数据的地方,自定义您的自我
for (int i=0;i<[finalresult count];i++)
NSLog(@"%@",[[finalresult objectAtIndex:i] objectForKey:@"name"];
【讨论】:
self. self.finalresult
?不清楚你为什么用了两次self
。
哦,对不起,请稍等,我更新了答案
@Kampai -- 你给了下载投票,你能解释一下我犯了什么错误
根据你的回购 - 你必须解释你做了什么。你还没有解释把这个 for 循环放在哪里。你也没有按照OP的需要回答。
@Anbu.Karthik 请告诉我,我可以在你的代码中传递我的 PHP url,谢谢。以上是关于Objective C从PHP获取JSON [重复]的主要内容,如果未能解决你的问题,请参考以下文章
从 TableView Objective C 中获取 IndexPath.Row [重复]
如何从 json-rpc webservice 获取数据:iPad /iPhone / Objective C