简体   繁体   English

如何显示带有换行符的从oracle数据库获取的JSON输出

[英]how to display JSON output fetched from the oracle database with line breaks

I have an HTML page which is has one user input and one button. 我有一个HTML页面,其中包含一个用户输入和一个按钮。 After clicking on the button, we can fetch other data related to that user from oracle database. 单击按钮后,我们可以从oracle数据库中获取与该用户有关的其他数据。 i have defined my code in a seperate php page( index2.php ) which looks like below:- 我已经在一个单独的php页面(index2.php)中定义了我的代码,如下所示:-

<?php
class logAgent
{
    const CONFIG_FILENAME = "data_config.ini";

    private $_dbConn;
    private $_config;


    function __construct()
    {
        $this->_loadConfig();


        $this->_dbConn = oci_connect($this->_config['db_usrnm'],
            $this->_config['db_pwd'],
            $this->_config['hostnm_sid']);
    }
    private function _loadConfig()
    {
        // Loads config
        $path = dirname(__FILE__) . '/' . self::CONFIG_FILENAME;
        $this->_config = parse_ini_file($path) ;
    }
    public function fetchLogs() {
        $userid =$_POST["userid"];
        $sql = "SELECT REQUEST_TIME,WORKFLOW_NAME,EVENT_MESSAGE
                            FROM AUTH_LOGS WHERE USERID = '".$userid."'";
        //Preparing an Oracle statement for execution
        $statement = oci_parse($this->_dbConn, $sql);

        //Executing statement
        oci_execute($statement);
        $json_array = array(); 

        while (($row = oci_fetch_row($statement)) != false) {
            $rows[] = $row;
            $json_array[] = $row; 
        }
            echo json_encode($json_array);
    }

}
    $logAgent = new logAgent();
    $logAgent->fetchLogs();
?>

I am doing this using asynchronous post method my defining a javascript file(script.js) 我正在使用异步发布方法来执行此操作,定义了一个javascript文件(script.js)

 $(document).ready(function(){ $("#mybtn").click(function(e){ e.preventDefault(); var userid = $("#userid").val(); //$.post("index2.php", {"userid" : userid}) var posting = $.post( 'index2.php', {"userid" : userid}); posting.done(function( res ) { var response = $.parseJSON(res); $('#rawResponse').text(response); }); }); }) 

I have many data for one user. 我有一个用户的许多数据。 And output display looks like: 03-SEP-18,softOtpCheck,OTP check passed,03-SEP-18,createAuthenticatedSession,yoya session Created.,03-SEP-18,createAuthenticatedSession,yoya session Created.,03-SEP-18,oamAuth,OAM context cookie collected.,03-SEP-18,softOtpCheck,OTP check passed,03-SEP-18,createAuthenticatedSession,yoya session Created.,03-SEP-18,createAuthenticatedSession,yoya session Created.,03-SEP-18,oamAuth,OAM context cookie collected. 输出显示如下:03-SEP-18,softOtpCheck,通过了OTP检查,03-SEP-18,createAuthenticatedSession,创建了yoya会话。,03-SEP-18,createAuthenticatedSession,创建了yoya会话,, 03-SEP-18, oamAuth,OAM上下文cookie已收集。,03-SEP-18,softOtpCheck,OTP检查已通过,03-SEP-18,createAuthenticatedSession,yoya会话已创建。,03-SEP-18,createAuthenticatedSession,yoya会话已创建。,03-SEP- 18,oamAuth,OAM上下文cookie收集。

But i want it as : 但我希望它是:

03-SEP-18,softOtpCheck,OTP check passed, 03-SEP-18,softOtpCheck,OTP检查通过,

03-SEP-18,createAuthenticatedSession,yoya session Created., 03-SEP-18,createAuthenticatedSession,yoya会话已创建。,

03-SEP-18,createAuthenticatedSession,yoya session Created., 03-SEP-18,createAuthenticatedSession,yoya会话已创建。,

03-SEP-18,oamAuth,OAM context cookie collected., 03-SEP-18,oamAuth,OAM上下文cookie已收集。,

03-SEP-18,softOtpCheck,OTP check passed, 03-SEP-18,softOtpCheck,OTP检查通过,

03-SEP-18,createAuthenticatedSession,yoya session Created., 03-SEP-18,createAuthenticatedSession,yoya会话已创建。,

03-SEP-18,createAuthenticatedSession,yoya session Created., 03-SEP-18,createAuthenticatedSession,yoya会话已创建。,

03-SEP-18,oamAuth,OAM context cookie collected. 03-SEP-18,oamAuth,OAM上下文cookie已收集。

How can i display my output in a newline? 如何在换行符中显示我的输出?

如果要回显json数据,则可以使用JSON_PRETTY_PRINT返回带有换行符的美化数据。

echo json_encode($json_array, JSON_PRETTY_PRINT);

I believe you want to change 我相信你想改变

$('#rawResponse').text(response);

to

$('#rawResponse').html('<pre>' + JSON.stringify(response, null, 2) + '</pre>);

See JSON.stringify and the enclosing pre tags will display the return value nicely. 参见JSON.stringify ,其中的pre标签将很好地显示返回值。 Alternatively you could also put it into a textarea , it will also display the newlines properly. 另外,您也可以将其放入textarea ,它也会正确显示换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM