将oci_fetch_assoc数据转换为json

时间:2019-03-14 04:37:55

标签: php json

我正在尝试从Oracle表中获取数据并转换为JSON对象,以便可以在Jquery Datatable上读取它。这是我的PHP代码-

    <?php

   $db = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1521)(SEND_BUF_SIZE=)(RECV_BUF_SIZE=))(LOAD_BALANCE=yes))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)))" ;

    if($c = OCILogon("test", "test", $db))
    {

        $rowfetch = oci_parse($c, "select * from table" );
        oci_execute($rowfetch);
    while($row=oci_fetch_assoc($rowfetch))
            {
                $arr['data'][]= $row;
                $senddata= json_encode($arr, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
                echo $senddata;
}
}
    else
    {
        $err = OCIError();
        echo "Connection failed." . $err[text];
    }


OCILogoff($c);
?>

我正在收到这样的JSON响应-

  

{“数据”:[{“ SCHEMA”:“ TEST”,“ TABLE_NAME”:“ TEST_T”,“ CUSTOMFIELD10”:“正在创建”,“ CUSTOMFIELD5”:“ NIK”,“ CUSTOMFIELD9”:“ NIK”, “ LAST_UPD_TIME”:“ 27-JUN-18”}]} {“数据”:[{“ SCHEMA”:“ TEST”,“ TABLE_NAME”:“ TEST_T”,“ CUSTOMFIELD10”:“创建中”,“ CUSTOMFIELD5”:“ NIK“,” CUSTOMFIELD9“:” NIK“,” LAST_UPD_TIME“:” 27-JUN-18“},{” SCHEMA“:” TEST“,” TABLE_NAME“:” ART_CM3_T“,” CUSTOMFIELD10“:”平均DWP“, “ CUSTOMFIELD5”:空,“ CUSTOMFIELD9”:“ BILLING”,“ LAST_UPD_TIME”:“ 05-FEB-19”}]} {“ data”:[{“ SCHEMA”:“ TEST”,“ TABLE_NAME”:“ TEST_T” ,“ CUSTOMFIELD10”:“正在创建”,“ CUSTOMFIELD5”:“ NIK”,“ CUSTOMFIELD9”:“ NIK”,“ LAST_UPD_TIME”:“ 27-JUN-18”},{“ SCHEMA”:“ TEST”,“ TABLE_NAME” :“ ART_CM3_T”,“ CUSTOMFIELD10”:“平均DWP”,“ CUSTOMFIELD5”:null,“ CUSTOMFIELD9”:“ BILLING”,“ LAST_UPD_TIME”:“ 05-FEB-19”},{“ SCHEMA”:“ TEST”, “ TABLE_NAME”:“ ART_T”,“ CUSTOMFIELD10”:“创建”,“ CUSTOMFIELD5”:“ NIK”,“ CUSTOMFIELD9”:“ NIK”,“ LAST_UPD_TIME”:“ 09-FEB-19”}}} {“数据” :[{“ SCHEMA”:“ TEST”,“ TABLE_NAME”:“ TEST_T”,“ CUSTOMFIELD10”:“ CREATING”,“ CUSTOMFIELD5”:“ NIK”,“ CUSTOMFIELD9”:“ NIK”,“ LAST_UPD_T IME“:” 27-JUN-18“},{” SCHEMA“:” TEST“,” TABLE_NAME“:” ART_CM3_T“,” CUSTOMFIELD10“:” Average DWP“,” CUSTOMFIELD5“:null,” CUSTOMFIELD9“:” BILLING “,” LAST_UPD_TIME“:” 05-FEB-19“},{” SCHEMA“:” TEST“,” TABLE_NAME“:” ART_T“,” CUSTOMFIELD10“:”创建“,” CUSTOMFIELD5“:” NIK“,” CUSTOMFIELD9 “:” NIK“,” LAST_UPD_TIME“:” 09-FEB-19“},{” SCHEMA“:” TEST“,” TABLE_NAME“:” ART_CUST“,” CUSTOMFIELD10“:”创建“,” CUSTOMFIELD5“:” NIK “,” CUSTOMFIELD9“:” NIK“,” LAST_UPD_TIME“:” 09-FEB-19“}]}

但这不是Datatable可以读取和接受的正确格式。对于Jquery Datatable,JSON响应应该像这样-

  

{     “数据”:[       [         “老虎尼克松”,         “系统架构师”,         “爱丁堡”,         “ 5421”,         “ 2011/04/25”,         “ 320,800美元”       ],       [         “ Garrett Winters”,         “会计”,         “东京”,         “ 8422”,         “ 2011/07/25”,         “ $ 170,750”       ],       [         “赫罗德·钱德勒”,         “销售助理”,         “旧金山”,         “ 9608”,         “ 2012/08/06”,         “ 137,500美元”       ],       [         “罗纳·戴维森(Rhona Davidson)”,         “集成专家”,         “东京”,         “ 6200”,         “ 2010/10/14”,         “ 327,900美元”       ],       [         “科琳·赫斯特”,         “ JavaScript开发人员”,         “旧金山”,         “ 2360”,         “ 2009/09/15”,         “ $ 205,500”       ],       [         “海莉·肯尼迪”,         “高级营销设计师”,         “伦敦”,         “ 3597”,         “ 2012/12/18”,         “ 313,500美元”       ],       [         “ Tatyana Fitzpatrick”,         “区域总监”,         “伦敦”,         “ 1965”,         “ 2010/03/17”,         “ 385,750美元”       ]     ]   }

我在这里做错了什么?如何将此JSON格式更改为所需的数据表格式?

1 个答案:

答案 0 :(得分:1)

在附加返回的记录后的while循环中,将其回显。

除了返回错误的格式外,还返回重复的记录。

您可以在对单个行进行编码后输出并输出包含在其中的已编码JSON数组。

echo '{"data": [';

$row = oci_fetch_row($rowfetch);
while ($row) {
    $senddata = json_encode($row, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);

    echo $senddata;

    $row = oci_fetch_row($rowfetch);

    if ($row === FALSE) break;

    echo ',';
}

echo ']}';
// Remember to free the statement and close the connection.

您还可以构建编码记录的数组并一起输出。

$records = [];

oci_fetch_all($rowfetch, &$records, null, null, OCI_FETCHSTATEMENT_BY_ROW + OCI_NUM);

$resp = ["data" => $records];

echo json_encode($resp, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
// Remember to free the statement and close the connection.