我知道这是一个非常古老的问题。但是我对这个概念很新。所以我只是在问...... 这是我的PHP代码
<?php
$host = "localhost";
$user = "xcode";
$pass = "xcode";
$db="xcode";
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}
echo mysql_get_server_info() . "\n";
$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "database selected\n";
}
$result1 = mysql_query("SELECT * FROM Persons");
$result=array();
while($row = mysql_fetch_array($result1))
array_push($result,array('firstName' => $row[0],'lastname'=>$row[1]));
echo json_encode(array("key" => $result));
mysql_close();
?>
和我的控制台值
Connection established 5.5.34 database selected {"key":[{"firstName":"Peter","lastname":"Griffin"},{"firstName":"Glenn","lastname":"Quagmire"},{"firstName":"Peter","lastname":"Griffin"},{"firstName":"Glenn","lastname":"Quagmire"}]}
我需要在Xcode的文本字段中获取此值 名字:普拉亚 名字:格维 提前谢谢。
答案 0 :(得分:1)
不确定你是否要求这里,所以我将包括下载JSON的代码和用于将JSON的对象设置为UITextFields的代码。
下载JSON对象的代码:
- (id)makeURLRequestWithString:(NSString *)url {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
NSURLResponse *response;
NSError *error;
// Make synchronous request
NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (urlData) {
NSDictionary *dict = [NSJSONSerialization
JSONObjectWithData:urlData
options:kNilOptions
error:&error];
return dict;
} else return error;
}
调用Web请求并获取结果的代码:
- (IBAction)requestJSON:(id)sender {
dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);
dispatch_async(jsonParsingQueue, ^{ //Do request on another queue to prevent it from blocking UI
id result = [self makeURLRequestWithString:@"http://www.blablabla.com/blabla.php"]; //Remember to include "http://"
[self performSelectorOnMainThread:@selector(results:) withObject:result waitUntilDone:YES];
});
}
- (void)results:(id)result {
//Check if the id is an NSDictionary, otherwise treat it as an error
if ([result isKindOfClass:[NSDictionary class]]) {
NSArray *key = [result objectForKey:@"key"];
NSDictionary *firstName = [key firstObject];
self.yourFirstTextField.text = [firstName objectForKey:@"firstName"];
self.yourSecondTextField.text = [firstName objectForKey:@"lastname"];
//OR use this code to get all first and last names in the same UITextField:
for(NSDictionary *dict in key) {
self.yourTextField.text = [NSString stringWithFormat:@"%@\n%@\n%@", self.YourTextField.text, [dict objectForKey:@"firstName"], [dict objectForKey:@"lastname"]];
}
} else {
//An NSError is returned. Handle it.
}
}
我从我自己的一个项目中复制了这段代码并对其进行了一些修改以适应这项任务,但要注意可能存在一些拼写错误或简单的逻辑错误: - )
编辑:
我的PHP经验非常少,因此如果需要一些特殊的POST或GET请求或其他东西来使服务器发送JSON,则必须修改makeUrlRequestWithString
方法以适应这一点。