PHP json_encode 反斜杠和数组名称的问题

发布于 2024-10-21 03:59:30 字数 1507 浏览 3 评论 0原文

我正在将一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-10-28 03:59:30

对于第一点,如果我尝试这样做:

$str = "this / string";
var_dump(json_encode($str));

我得到:

string '"this \/ string"' (length=16)

也带有反斜杠。

看看 json.org,似乎 JSON 标准定义字符串内的斜杠应该被逃脱。

所以, json_encode() 似乎做了正确的事情。

如果您不希望这些斜杠被转义,那么您不需要 valid-JSON,并且不应该使用 json_encode


对于第二点,现在,您不应该使用:

$posts[] = array(..., $posts2 );

相反,您应该使用:

$posts[] = array(..., 'attach' => $posts2 );

这样,数组的最后一个元素将具有“attach”名称。

For the first point, if I try doing this :

$str = "this / string";
var_dump(json_encode($str));

I get :

string '"this \/ string"' (length=16)

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 :

$posts[] = array(..., $posts2 );

Instead, you should use :

$posts[] = array(..., 'attach' => $posts2 );

This way, that last element of the array will have the 'attach' name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文