所以,我通过隐藏的表单字段传递一个数组,并使用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
?>
根据我在这篇文章中收到的评论,我实施了一些错误检查
好的,谢谢,所以最后一个错误返回 Code 4 ,这可能是由一个坏字符引起的。
jsonlint作为未定义的函数失败。最后一条错误消息返回空白。
好的,如果这与我的json中的一个坏角色有关,那么2个问题 - 为什么我的测试脚本没有失败?我如何解决错误的字符错误,因为我无法预测下次抓取数据时会出现什么样的错误字符。因为它都是动态的。
答案 0 :(得分:0)
好吧,我深究了它。
因为我在前面的步骤中弄乱了htmlspecialchars,所以我不得不摆脱转义字符\以使json对象有效。
所以我更换了这个:
$datajson2 = str_replace('"','"',$datajson);
有了这个:
$datajson2 = preg_replace('/\\\"/',"\"", $datajson);
至于对我的问题进行投票的人,如果您认为这是对stackoverflow社区有用的最佳方式,那么请继续。这不会以任何形式或形式影响我。我不是来这里赚取布朗尼积分。完成工作,案件结案!