簡體   English   中英

CodeIgniter和RESTful:如何從返回的json中刪除反斜杠?

[英]CodeIgniter and RESTful: how to remove backslashes from returned json?

我正在努力使用PhilSturgeon REST Server從CodeIgniter中的json響應中刪除轉義字符。

一切正常,但是問題出在響應上,當我訪問URL以json格式獲取數據時,得到了,但轉義了字符。

例:

http://localhost/revista_servidor/index.php/api/notas/nota/id/1

給我下一個回應:

[{{“ id”:“ 1”,“ autor”:“ Prueba autor”,“ titulo”:“ Comprobaci \\”,“ subtitulo”:“ Comprobaci \\”。 ,“ foto1”:“ http://link.a.foto/foto1”,“ texto1”:“ Comprobaci \\ u00f3n de texto 1. \\ r \\ n”,“ pauta1”:“ 1”,“ texto2”:“ Comprobaci \\ u00f3n de texto2。\\ r \\ n“,” foto2“:” http://link.a.foto/foto2“,” pauta2“:” 1“,” texto3“:” Comprobaci \\ u00f3n de texto 3 。“,” foto3“:” http://link.a.foto/foto3“,” pauta3“:” 1“,” texto4“:” Comprobaci \\ u00f3n de texto 4“ ,,” texto5“:” Comprobaci \\ u text 5。“,” texto6“:” Comprobaci \\ u00f3n de texto 6.“,” datosweb“:” http://link.a.pagina.de.datos/“,” adelanto“:” Comprobaci \\ u00f3n del texto de adelante“,” nrorevista“:” 69“}]

它使用轉義添加反斜杠\\並更改特殊字符(在本示例中為ó )的

我試過添加stripslashes()但沒有用。 . 我檢查了開發人員工具中的響應,結果與預期的一樣:

如何解決此編碼問題? 我還檢查了配置文件,對於此問題似乎沒有任何更改。

我希望有人可以指出正確的方向,以下是我的代碼:

控制器: /application/controllers/api/notas.php

    function nota_get() {
    // ID verification.
    if ( !$this->get('id') ) {
        // NO ID.
        $this->response(NULL, 400);
    }

    $nota = $this->Notas_model->get( $this->get('id') );

    if ($nota) {
            stripcslashes($this->response($nota, 200));
    }

    else {
        $this->response(NULL, 404);
    }
}

型號: /application/models/notas_model.php

function get($id = 0) {
    $this->load->database();
    if ( $id ) {
        $query = $this->db->get_where( 'notas', array('id' => $id) );
    }

    else {
        $query = $this->db->get('notas');
    }
    return $query->result();
}

我不知道這是否重要,但是可以通過客戶端中的javascript訪問此數據。

提前致謝!

它只是JSON工作的方式,請嘗試json_decode函數。 例如:

$json = json_decode($json_string);
$json->autor;

1)您需要先使用json_decode()然后使用urldecode(),而不是stripslashes()

2)urldecode()和stripslashes()都將字符串作為參數,而您試圖將其作為對象時,該對象將“自動縮減”為依賴於PHP版本的對象……無論是什么,它都是可能不是您所期望的。

在您的代碼中:

if ($nota) {
        stripcslashes($this->response($nota, 200));
}

您將需要a)將解碼結果保存到相同或另一個變量中,b)遍歷對象($ nota)並取消轉義每個鍵值對中的值。

嘗試

...
$cleanObject = array();

if ($nota) {

    $decodedObject = json_decode($this->response($nota, 200));

    foreach ( $decodedObject as $key => $value ) {

      $cleanObject[$key] = urldecode( $value );

    }
}

echo "<pre>";
print_r($cleanedObject);
echo "</pre>";

// output 
/* 
Array
(
[id] => 1
[autor] => Prueba autor
[titulo] => Comprobación de tíítulo.
[subtitulo] => Comprobación de subtítulo.
[foto1] => http://link.a.foto/foto1
[texto1] => Comprobación de texto 1.

[pauta1] => 1
[texto2] => Comprobación de texto 2.

[foto2] => http://link.a.foto/foto2
[pauta2] => 1
[texto3] => Comprobación de texto 3.
[foto3] => http://link.a.foto/foto3
[pauta3] => 1
[texto4] => Comprobación de texto 4.
[texto5] => Comprobación de texto 5.
[texto6] => Comprobación de texto 6.
[datosweb] => http://link.a.pagina.de.datos/
[adelanto] => Comprobación del texto de adelante
[nrorevista] => 69
)
*/


...

希望這是您要實現的目標。

根據您的進一步需求,您可能必須重新編碼結果。 根據您提到的javascript,然后可能需要將結果轉換回JSON:

$unescapedAndJSONencodedObject = json_encode( $cleanObject );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM