douyinlai2169 2016-03-10 16:21
浏览 37

json_decode同时在同一台服务器上工作和失败

So, I'm passing an array via a hidden form field and using json to get it across correctly. When I decode the json after passing, it fails.

I made a quick sample file on the same server to test the ins and outs of json decoding and to my sheer horror it is working. Same code, same server. How does it decide when to work and when to fail?

Any help would be greatly appreciated.

This is the array that I encode before passing it via form

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
        )
)

This is the form code

<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>

This is the bit that captures the posted data

<?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
       }
?>

And this is the test code that is working ok

<?
$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
?>

Based on the comments I received on this post, I implemented some error checking

ok, thanks, so the last error returns Code 4 which is probably caused by a bad character.

jsonlint fails as an undefined function. last error message comes back blank.

Ok, if this is to do with a bad character in my json, 2 questions - why does it not fail on my test script? And how do I solve the bad character error because I can't predict what bad character will there be next time it grabs data. Because it's all dynamic.

  • 写回答

1条回答 默认 最新

  • doupu2722 2016-03-11 22:31
    关注

    All right, I got to the bottom of it.

    Because I was messing with htmlspecialchars in the previous steps, I had to get rid of the escape character \ for the json object to be valid.

    So I replaced this:

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

    With that:

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

    As for the person who downvoted my question, well, if you think this is the best way for you to be useful to the stackoverflow community, then go ahead. This doesn't affect me in any way shape or form. I'm not here to earn brownie points. Job done, case closed!

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号