更新1:字符串處理,去掉了一個逗號
這是我在嘗試建立字符串:
header('Content-type:application/json');
function getdata($the_query)
{
$connection = mysql_connect('server', 'user', 'pass') or die (mysql_error());
$db = mysql_select_db('db_name', $connection) or die (mysql_error());
$results = mysql_query($the_query) or die(mysql_error());
$the_data = "{
\"rss\": {
\"channels\" : [
{
\"title\" : \"".$title."\",
\"link\": \"http://www.mycompany.com/external.php\",
\"description\": \"company description goes here\",";
while($row = mysql_fetch_array($results))
{
extract($row);
$the_data .= "\"items\" : [
{
\"title\": \"".$title."\",
\"link\": \"".$link."\",
\"guid\": \"".$link."\",
\"pubDate\": \"".$date."\",
\"description\": \"".$description."\"
} ],";
}
$the_data .= "} ]
}
}";
mysql_close($connection);
return $the_data;
}
原題:
我有一個字符串類似於此:
$mystring = "{
\"rss\": {
\"channels\" : [
{
\"title" : \"title goes here\",
\"link": \"link goes here\",
\"description": \"description goes here\",
\"items\" : [
{
\"title\": \"title goes here\",
\"link\": \"url goes here\",
\"guid\": \"id goes here\",
\"pubDate\": \"data goes her\",
\"description\": \"description goes here\"
} ],
\"items\" : [
{
\"title\": \"title goes here\",
\"link\": \"url goes here\",
\"guid\": \"id goes here\",
\"pubDate\": \"data goes her\",
\"description\": \"description goes here\"
} ],
\"items\" : [
{
\"title\": \"title goes here\",
\"link\": \"url goes here\",
\"guid\": \"id goes here\",
\"pubDate\": \"data goes her\",
\"description\": \"description goes here\"
} ],
} ]
}
}";
如何刪除最後一個逗號?
我想不出一個簡單的方法,因爲'json_decode()'不允許尾隨逗號。爲什麼它在那裏擺在首位?數據來自哪裏? – 2011-06-02 08:44:42
看起來像JSON,你能否引用你的來源。 – doNotCheckMyBlog 2011-06-02 08:45:59
也許我不是這樣做的最好方式,但該字符串(項目部分)是在一個循環中,因此尾隨逗號。什麼是創建json數據的正確方法? – oshirowanen 2011-06-02 08:49:16