【问题标题】:json_decode working and failing on the same server at the same timejson_decode 在同一台服务器上同时工作和失败
【发布时间】:2016-06-25 14:44:59
【问题描述】:

所以,我通过一个隐藏的表单字段传递一个数组,并使用 json 来正确传递它。当我通过后解码json时,它失败了。

我在同一台服务器上制作了一个快速示例文件,以测试 json 解码的来龙去脉,令我恐惧的是它正在工作。相同的代码,相同的服务器。它如何决定何时工作,何时失败?

任何帮助将不胜感激。

这是我在通过表单传递之前编码的数组

Array
(
    [0] => Array
        (
            [cont_twit] => BillionDollarID
            [cont_name] => BillionDollar-ID
            [cont_foll] => 382903
            [cont_frnd] => 296230
            [cont_ftf] => 1.29
            [cont_inact] => Last tweeted today
        )

    [1] => Array
        (
            [cont_twit] => CrimeConspiracy
            [cont_name] => MI6 Crown Carroll
            [cont_foll] => 452638
            [cont_frnd] => 441748
            [cont_ftf] => 1.02
            [cont_inact] => Last tweeted today
        )

    [2] => Array
        (
            [cont_twit] => 107KingTN
            [cont_name] => TN
            [cont_foll] => 6505
            [cont_frnd] => 6360
            [cont_ftf] => 1.02
            [cont_inact] => Last tweeted today
        )
)

这是表单代码

<form action="" id="project" method="POST">
   <input type="hidden" value="<?php echo htmlspecialchars(json_encode($dataarray)); ?>" name="bigarray">
   <input name="projectsub" type="submit" value="Create Project"></p>
</form>

这是捕获发布数据的位

<?php
        if(isset($_POST['projectsub'])){
            $datajson = ($_POST['bigarray']);
            echo "<br>datajson ".$datajson; //output is messed up
            $datajson2 = str_replace('&quot;','"',$datajson);
            echo "<br> datajson2 ".$datajson2; //output is ok
            $dataarray = json_decode($datajson2);
            echo"<br> data: ";
            print_r($dataarray); //output is blank
       }
?>

这是正常工作的测试代码

<?
$json_object = "[{&quot;cont_twit&quot;:&quot;BillionDollarID&quot;,&quot;cont_name&quot;:&quot;BillionDollar-ID&quot;,&quot;cont_foll&quot;:382902,&quot;cont_frnd&quot;:296230,&quot;cont_ftf&quot;:1.29,&quot;cont_inact&quot;:&quot;Last tweeted today&quot;},{&quot;cont_twit&quot;:&quot;CrimeConspiracy&quot;,&quot;cont_name&quot;:&quot;MI6 Crown Carroll&quot;,&quot;cont_foll&quot;:452637,&quot;cont_frnd&quot;:441749,&quot;cont_ftf&quot;:1.02,&quot;cont_inact&quot;:&quot;Last tweeted today&quot;},{&quot;cont_twit&quot;:&quot;107KingTN&quot;,&quot;cont_name&quot;:&quot;TN&quot;,&quot;cont_foll&quot;:6505,&quot;cont_frnd&quot;:6360,&quot;cont_ftf&quot;:1.02,&quot;cont_inact&quot;:&quot;Last tweeted today&quot;}]";

$json_object2 = str_replace('&quot;','"',$json_object);

$json_object3 = '[{"cont_twit":"BillionDollarID","cont_name":"BillionDollar-ID","cont_foll":382903,"cont_frnd":296230,"cont_ftf":1.29,"cont_inact":"Last tweeted today"},{"cont_twit":"CrimeConspiracy","cont_name":"MI6 Crown Carroll","cont_foll":452638,"cont_frnd":441748,"cont_ftf":1.02,"cont_inact":"Last tweeted today"},{"cont_twit":"107KingTN","cont_name":"TN","cont_foll":6505,"cont_frnd":6360,"cont_ftf":1.02,"cont_inact":"Last tweeted today"}]';

$json_object4 = "[{\"cont_twit\":\"BillionDollarID\",\"cont_name\":\"BillionDollar-ID\",\"cont_foll\":382902,\"cont_frnd\":296230,\"cont_ftf\":1.29,\"cont_inact\":\"Last tweeted today\"},{\"cont_twit\":\"CrimeConspiracy\",\"cont_name\":\"MI6 Crown Carroll\",\"cont_foll\":452637,\"cont_frnd\":441749,\"cont_ftf\":1.02,\"cont_inact\":\"Last tweeted today\"},{\"cont_twit\":\"107KingTN\",\"cont_name\":\"TN\",\"cont_foll\":6505,\"cont_frnd\":6360,\"cont_ftf\":1.02,\"cont_inact\":\"Last tweeted today\"}]";
$decoded_object = json_decode($json_object);
print_r($decoded_object); //this fails for obvious reasons
echo "<br/>";
$decoded_object2 = json_decode($json_object2);
print_r($decoded_object2); //this works perfectly
echo "<br/>";
$decoded_object3 = json_decode($json_object3);
print_r($decoded_object3); //this works perfectly
echo "<br/>";
$decoded_object4 = json_decode($json_object4);
print_r($decoded_object4); //this works perfectly
?>

根据我在这篇文章中收到的 cmets,我实施了一些错误检查

好的,谢谢,所以最后一个错误返回 Code 4,这可能是由错误字符引起的。

jsonlint 作为未定义函数失败。最后一条错误消息返回空白。

好的,如果这与我的 json 中的一个坏字符有关,2 个问题 - 为什么它在我的测试脚本上没有失败?以及如何解决坏字符错误,因为我无法预测下次抓取数据时会出现什么坏字符。因为它都是动态的。

【问题讨论】:

  • 调用 json_last_error() 来调试它(如果是语法错误,可能类似于 jsonlint)
  • 或者使用json_last_error_msg()来显示一个人类可读的错误。
  • 好的,谢谢,我已经实施了错误检查并将我的发现添加到主帖中。

标签: php json regex


【解决方案1】:

好吧,我查到了。

因为我在前面的步骤中弄乱了 htmlspecialchars,所以我必须去掉转义字符 \ 才能使 json 对象有效。

所以我替换了这个:

$datajson2 = str_replace('&quot;','"',$datajson);

这样:

$datajson2 = preg_replace('/\\\"/',"\"", $datajson);

至于对我的问题投反对票的人,好吧,如果您认为这是对 stackoverflow 社区有用的最佳方式,那么请继续。这不会以任何方式影响我的形状或形式。我不是来这里赚取布朗尼积分的。工作完成,案件结案!

【讨论】:

  • 这没有任何意义。为什么你在发布的数据中得到&amp;quot;?当您将 JSON 放入 HTML 时,您是否将其 htmlencode()htmlspecialchars() 两次?您从 JSON 中删除的额外反斜杠来自配置不正确的 PHP (magic_quotes_gpc=on)。您使用的是什么版本的 PHP? magic_quotes_gpc 已在 PHP 5.4 中删除。无论如何,你应该使用stripslashes()来取消magic_quotes_gpc的效果。
  • axiac,这是我整个星期读过的最有帮助的东西。我现在就解决这个问题,谢谢。我在 PHP 5.3 上(是的,我知道我知道)
  • 为了安全起见,您必须使用get_magic_quotes_gpc() 检查magic_quotes_gpc ini 设置的状态,并仅在返回TRUE 时使用stripslashes()if (get_magic_quotes_gpc()) { $datajson = stripslashes($datajson); } $dataarray = json_decode($datajson, TRUE);。将TRUE 作为第二个参数传递给json_decode() 要求它返回一个数组(默认行为是返回一个对象)。
猜你喜欢
  • 2019-08-11
  • 2018-07-07
  • 2019-11-03
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 2021-11-20
相关资源
最近更新 更多