I'm rather new to ios and json.
I've managed to create a db in mysql and used php to create a basic read and write php pages.
I'm using the TwitterHelper class from cs193p class for the presence assignment from Stanford to acces the p开发者_如何学Chp interface online and tie it to my ios code. I am getting an error which I can't solve.
Update:
Ok, here is my code:
<?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).'}';
?>
The problem is that unless i remove the [] from $arr[], i get the answer to the php page enclosed in [] which the json fetch doesnt like and therefore throws and exception error and crashes my app.
If i remove it, the php page only returns 1 result...
What does Services_JSON
do? In particular Service_JSON->encode()
. And would a simple json_encode
do the job? (since you do nothing else with the Service_JSON
Object anyway)
Edit
$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}
Accordig to the PEAR::Services_JSON::docs
$json = new Service_JSON;
echo $json->encode($arr);
output: ["a":1,"b":2,"c":3,"d":4,"e":5]
the while loop is an iterator. if you want to fetch all the value from the database then the echo statement should be specified within the while loop.
精彩评论