我对 ios 和 json 比较陌生。
我设法在 mysql 中创建了一个数据库,并使用 php 创建了一个基本的读写 php 页面。
我正在使用 cs193p 类中的 TwitterHelper 类进行斯坦福的在线分配,以在线访问 php 界面并将其绑定(bind)到我的 ios 代码。我遇到无法解决的错误。
更新:
好的,这是我的代码:
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("mydb") or die("Could not select database");
$query = "SELECT * FROM tags";
$result = mysql_query($query);
$arr = array();
$rs = mysql_query("SELECT * FROM tags");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
//Echo '{:'.$json->encode($arr).'}';
?>
问题是,除非我从 $arr[] 中删除 [],否则我会得到包含在 [] 中的 php 页面的答案,json 提取不喜欢它,因此会抛出异常错误并使我的应用程序崩溃。
如果我删除它,php 页面只返回 1 个结果...
最佳答案
Services_JSON
有什么作用?特别是 Service_JSON->encode()
。并且会是一个简单的 json_encode
做这份工作? (因为无论如何您都没有对 Service_JSON
对象执行任何其他操作)
编辑
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
header('Content-type: application/json');
echo json_ecode($arr);
output: {"a":1,"b":2,"c":3,"d":4,"e":5}
$json = new Service_JSON;
echo $json->encode($arr);
output: ["a":1,"b":2,"c":3,"d":4,"e":5]
关于php - 带有 JSON、PHP 的 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3743007/