我有一个 iPhone 应用程序,它从 php 文件获取响应。此响应生成一个 html 文件,其中正文包含 json 对象。如何检索此 JSON 对象?
$query = "SELECT username FROM userData WHERE username = '$username'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
sendResponse(200, json_encode('SUCCESS Notification'));
} else {
$query = "INSERT INTO userData (username,password,email,signup_date) VALUES ('$username','$password','$email','$date')";
$result=mysql_query($query,$connection) or die (mysql_error()." Kann Tabelle der Datenbank nicht lesen!");
}
function sendResponse ($status = 200, $body ='', $content_type = 'application/json'){
$status_header = 'HTTP/1.1 ' . $status . ' ' . 'OK';
header($status_header);
header('Content-type:' . $content_type);
echo $body;
}
这是我的 iPhone 应用程序中发生的情况:
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&error];
NSDictionary *jsonDictionaryResponse =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"json response = %@", jsonDictionaryResponse);
最佳答案
据我所知,您没有在 JSON 编码字符串周围添加 HTML 标签。所以你可以解析整个响应主体。我建议您使用jQuery (如果您还没有这样做)或 JSON library .
但请注意,发送 JSON 数据时应将 MIME-Type 更改为 application/json
,并且对字符串进行 json_encode
没有意义。
sendResponse(200, json_encode(array('SUCCESS Notification')), 'application/json');
关于php - 如何从 iPhone 应用程序获取 html 文件的内容(JSON 对象)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960806/