我在php中创建了一个WebService,用于从mySQL DB获取数据并返回JSON。
所以,我需要这样,每当我的数据库中有null value
时,此值都会更改为白色(= " "
)。
我这样做了,但没有成功(看看最后foreach
):
<?php
header("Content-Type: application/json; charset=utf-8;");
include('connectdb.php');
$something = $_GET['cod'];
$sqlcode = mysql_query("Select descricao, cliente, local from terminal_cartao Where descricao='$something'");
$sqlcode2 = mysql_query("Select descricao, cliente, local from terminal_cartao");
$jsonObj= array();
if($something == 'all')
{
while($result=mysql_fetch_object($sqlcode2))
{
$jsonObj[] = $result;
}
}
else{
while($result=mysql_fetch_object($sqlcode))
{
$jsonObj[] = $result;
}
}
foreach ($jsonObj as $key => $value) {
if ($value === null) {
$jsonObj[$key] = "";
}
}
$final_res =json_encode($jsonObj);
echo $final_res;
exit;
答案 0 :(得分:1)
foreach ($jsonObj as $key => $value) {
$value = get_object_vars($value);
foreach($value as $key2 => $value2) {
if ($value2 === null) {
$jsonObj[$key]->$key2 = "";
}
}
}
答案 1 :(得分:0)
下面的代码对我来说很好用:
foreach ($jsonObj as $k1 => $row) {
foreach ($row as $k2 => $value) {
if ($value === null) {
$jsonObj[$k1]->$k2 = "";
}
}
}