开发者

JSON character encoding in php script

I am retrieving json from a php file that connects to the database and then creates the json to be imported into my page via an ajax call.

Some of the database columns have file paths in them, ie images/myfolder/myfile

The json that I see when I open the page in a web browser formats the file path like this:

\/images\/myfolder\/myfile

Are the escaped characters going to break the json?

i.e, if i do var icon_image = myData.file

will icon_image hold: \/images\/myfolder\/myfile or /images/myfolder/myfile

I am hoping the second option from above, but if it doesn't how do I get it to display as /images/myfolder/myfile

I have put mb_internal_encoding( 'UTF-8' ); at the top of the php page

The script that generates the json is as follows:

mb_internal_encoding( 'UTF-8' );
mysql_select_db($database_growth_conn, $growth_conn);
$query_rs_icons = sprintf("SELECT * FROM icons_ico ORDER BY name_ico");
//echo($query开发者_开发问答_rs_icons);
$rs_icons = mysql_query($query_rs_icons, $growth_conn) or die(mysql_error());
$row_rs_icons = mysql_fetch_assoc($rs_icons);
$totalRows_rs_icons = mysql_num_rows($rs_icons)
$rows = array();

while($r = mysql_fetch_assoc($rs_icons)) {
    $rows[] = $r;
}

$jsondata = json_encode($rows);
echo '{"icons":'.$jsondata.'}';


are the escaped characters going to break the json?

No. A backslash-escaped non-special punctuation is just the same as the punctuation itself. The JS string literals "a/b" and "a\/b" result in the same string value, a/b.

json_encode escapes the forward slash character for you so that if you try to include a string with the sequence </script> in it in a script element, it doesn't prematurely end the script block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜