PHP json_encode 反斜杠和数组名称的问题
我正在将一些 postgresql 数据转换为 PHP json_encode,但遇到了一些问题:
json_encode 向我数据中的所有斜杠添加了一个 BackSlash。
在描述中出现段落标记的结束,我认为是因为反斜杠问题...
我不'不希望我的数组位于以索引“0”命名但名称为“attach:”的对象内
我的 JSON 输出:
{"arns":[{"arn":"CSC-ECN-NAUB109","problem":"description problem<\/p>",
"solution":"solution description<\/p>",
"0":[{"name":"jquery.png","path":"http:\/\/arn.test.pt\/uploads\/CSC-ECN-NAUB109\/jquery.png"}]}]}
我的代码:
<?php
require('includes/connection.php');
$modelos_info = (isset($_GET['models'])) ? $_GET['models'] : "none";
if($modelos_info != "none"){
$sth = $dbh->query("SELECT * FROM arn_info JOIN upload2 ON (arn_info.arn=upload2.id_arn) WHERE modelos LIKE '$modelos_info ;%' OR modelos LIKE '%; $modelos_info ;%' ");
$sth->setFetchMode(PDO::FETCH_ASSOC);
$response = array();
$posts = array();
while($row = $sth->fetch())
{
$arn=$row ['arn'];
$problem=$row['problem'];
$solution=$row['solution'];
$name=$row['name'];
$path=$row['path'];
$posts_anexos['attach'] = $posts2;
$posts2[] = array('name'=> $name , 'path'=> 'http://arn.test.pt/' .$path);
$posts[] = array('arn'=> $arn , 'problem'=> $problem , 'solution'=> $solution, $posts2 );
}
$response['arns'] = $posts;
$fp = fopen('arns.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
echo json_encode($response);
}
?>
谢谢
I'm converting some postgresql data to PHP json_encode, but I have some problems:
json_encode adds an BackSlash to all slashes that I have in my data.
In descriptions apears the close of paragraph tag , i think because the backslashes problem...
and i don't want my array inside the object named with index "0" but with the name "attach:"
MY JSON OUTPUT:
{"arns":[{"arn":"CSC-ECN-NAUB109","problem":"description problem<\/p>",
"solution":"solution description<\/p>",
"0":[{"name":"jquery.png","path":"http:\/\/arn.test.pt\/uploads\/CSC-ECN-NAUB109\/jquery.png"}]}]}
MY CODE:
<?php
require('includes/connection.php');
$modelos_info = (isset($_GET['models'])) ? $_GET['models'] : "none";
if($modelos_info != "none"){
$sth = $dbh->query("SELECT * FROM arn_info JOIN upload2 ON (arn_info.arn=upload2.id_arn) WHERE modelos LIKE '$modelos_info ;%' OR modelos LIKE '%; $modelos_info ;%' ");
$sth->setFetchMode(PDO::FETCH_ASSOC);
$response = array();
$posts = array();
while($row = $sth->fetch())
{
$arn=$row ['arn'];
$problem=$row['problem'];
$solution=$row['solution'];
$name=$row['name'];
$path=$row['path'];
$posts_anexos['attach'] = $posts2;
$posts2[] = array('name'=> $name , 'path'=> 'http://arn.test.pt/' .$path);
$posts[] = array('arn'=> $arn , 'problem'=> $problem , 'solution'=> $solution, $posts2 );
}
$response['arns'] = $posts;
$fp = fopen('arns.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
echo json_encode($response);
}
?>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一点,如果我尝试这样做:
我得到:
也带有反斜杠。
看看 json.org,似乎 JSON 标准定义字符串内的斜杠应该被逃脱。
所以, json_encode() 似乎做了正确的事情。
如果您不希望这些斜杠被转义,那么您不需要 valid-JSON,并且不应该使用
json_encode
。对于第二点,现在,您不应该使用:
相反,您应该使用:
这样,数组的最后一个元素将具有“attach”名称。
For the first point, if I try doing this :
I get :
With backslashes too.
Looking at json.org, it seems the JSON standard defines that slashes, inside strings, should be escaped.
So,
json_encode()
seems to be doing the right thing.If you do not want those slashes to be escaped, then, you don't want valid-JSON, and should not work with
json_encode
.For the second point, now, you should not use this :
Instead, you should use :
This way, that last element of the array will have the 'attach' name.