2017-11-24 8 views
0

PHPプログラミングの新機能で、簡単なPHPで簡単なAPIを作成しようとしています。私のスニペットコードの下:私はフィールドshirtImageの値は、余分なシンボル\を持っている理由を知りたいJSONエンコード出力記号 " /" with PHP

{"response":[{"id":"31","shirtImage":"Content\/Images\/Short Sleeve\/874be7b82812f76c944d71706c9651eb.gif"},{"id":"32","shirtImage":"Content\/Images\/Short Sleeve\/b-Cleaned.png"}]} 

:上記

$con = mysqli_connect(HOST, USER, PASS, DB); 
$sql_results = mysqli_query($con, "SELECT * FROM `table-images`"); 
$rows = array(); 
while($r = mysqli_fetch_assoc($sql_results)) { 
    $rows[] = $r; 
} 
echo'{"response":'.json_encode($rows).'}'; 

コードは以下のようなJSON出力を持っています。私のデータベースでは、値は正しい例Content/Images/Short Sleeve/b-Cleaned.pngですが、JSONにエンコードすると出力が変更されています。これを修正するには?

私のケースに関するいくつかのキーワードが見つかりましたが、動作しません。どんな示唆と答えも役立ちます。ありがとうございました

答えて

1

json_encode()はスラッシュをエスケープします。試してみてください:

echo'{"response":'.json_encode($rows, JSON_UNESCAPED_SLASHES).'}'; 
+0

ありがとう、その仕事。 –

+0

嬉しいです。利用可能なすべてのオプションは、http://php.net/manual/en/function.json-encode.phpにあります。 –

0
Its due to escaping/with `\` and it will be removed when you decode your json back. 

これをデコードしてみてください。

$d = json_decode('{"response":[{"id":"31","shirtImage":"Content/Images/Short Sleeve/874be7b82812f76c944d71706c9651eb.gif"},{"id":"32","shirtImage":"Content/Images/Short Sleeve/b-Cleaned.png"}]}'); 

print_r($d);die; // This will give you the original data with out `\` 
+0

私はそのJSONをデコードすると、Java Androidで処理できることがわかります。しかし、 'json_encode's phpはそのシンボルを削除できますか? –

+0

はい、そうです。やってみなよ。 –

関連する問題