【发布时间】: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('"','"',$datajson);
echo "<br> datajson2 ".$datajson2; //output is ok
$dataarray = json_decode($datajson2);
echo"<br> data: ";
print_r($dataarray); //output is blank
}
?>
这是正常工作的测试代码
<?
$json_object = "[{"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"}]";
$json_object2 = str_replace('"','"',$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()来显示一个人类可读的错误。 -
好的,谢谢,我已经实施了错误检查并将我的发现添加到主帖中。