【问题标题】:Read associative array from json in $_POST从 $_POST 中的 json 读取关联数组
【发布时间】:2011-08-13 23:59:20
【问题描述】:

我正在使用 jQuery 将 json 对象发布到我的 php 应用程序。

jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });

从 firebug 中提取的 json 字符串如下所示

{ "data" : [ { "contents" : "This is some content",
        "selector" : "DIV.subhead"
      },
      { "contents" : "some other content",
        "selector" : "LI:nth-child(1) A"
      }
    ],
  "page" : "about_us.php"
}

在 php 中我试图把它变成一个关联数组。

到目前为止我的php代码是

<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>

对 ajax 调用的响应应该是“about_us.php”,但它返回空白。

【问题讨论】:

    标签: php json post http-post associative-array


    【解决方案1】:

    如果请求正文不是标准的 urlencoded 格式,则不会填充$_POST

    相反,像这样从read-only php://input stream 读取以获取原始请求正文:

    $value = json_decode(file_get_contents('php://input'));
    

    【讨论】:

    • 是真的,如果我的 POST 数据不是 data=value 内容就足够了,那么我将使用 file_get_contents() 而不是 $_POST ? @Evert?
    • 这取决于发送的内容类型。
    • 取决于内容类型?但是在我的代码中,我使用的是“Content-type”、“application/json”的内容类型,但是......在服务器本身中,我需要使用 file_get_contents() 而不是 $_POST。叹息。
    • 是的,因为 application/json 不是将填充 $_POST 的内容类型之一。只有 application/form-data 和 application/x-www-form-urlencoded 会导致它被解析。 file_get_contents 实际上是最好的方法,OP 最终使用的解决方案并不优雅。
    • @Evert $HTTP_RAW_POST_DATA 在 php 5.6 中已贬值。现在,每当我使用 file_get_contents('php://input')' 处理 json_encoded 数据(在我托管在 heroku 上的 php-scrpt 上)时,它都会抱怨它并希望我设置 'always_populate_raw_post_data' to '-1'。但如果我这样做,它将无法填充 $value 中的数据。现在该怎么办?
    【解决方案2】:

    你可以避免使用JSON.stringifyjson_decode

    jQuery.post("save.php", dataToSend, function(data){ alert(data); });
    

    还有:

    <?php
    echo $_POST['page'];
    ?>
    

    更新:

    ...但如果你真的想使用它们,那么:

    jQuery.post("save.php",  {json: JSON.stringify(dataToSend)}, function(data){ alert(data); });
    

    还有:

    <?php
    $value = json_decode($_POST['json']);
    echo $value->page;
    ?>
    

    【讨论】:

    • 谢谢您,您的第一种方法是迄今为止最简单的解决方案,并且有效!我昨天尝试了第二个,但无法正常工作。
    【解决方案3】:

    如果您想要关联数组,则将第二个参数作为 true 传递,否则它将继续返回对象。

    $value = json_decode(stripslashes($_POST),true);
    

    【讨论】:

    • 警告:stripslashes() 期望参数 1 是字符串,给定数组
    • 如果您要发送多个帖子变量并且其中一个是 json 字符串,则带斜杠非常棒
    • -1;怎么回事?这在任何情况下都不会奏效。 $_POST始终是一个数组,stripslashes从不接受一个数组作为参数。你给出的代码保证会抛出一个警告并将$value设置为null,无论POST正文如何。
    【解决方案4】:

    试试:

    echo $value->page;
    

    因为json_decode 的默认行为是返回stdClass 类型的对象。

    或者,将第二个可选的$assoc 参数设置为true

    $value = json_decode(stripslashes($_POST), true);
    echo $value['page'];
    

    【讨论】:

      【解决方案5】:

      看起来 jQuery 可能会以 urlencoded 形式对 javascript 对象进行编码,然后将其填充到 $_POST 中。至少来自他们的examples。我会尝试将您的对象传递到 post() 而不对其进行字符串化。

      【讨论】:

        【解决方案6】:

        如果你想使用json数据作为关联数组,你可以尝试如下:

        <?php 
        
        $json = 'json_data'; // json data
        
        $obj = jsondecode($json, true); // decode json as associative array 
        
        // now you can use different values as 
        echo $obj['json_string']; // will print page value as 'about_us.php' 
        
        
        for example: 
        
        $json = { "data" : [ { "contents" : "This is some content",
            "selector" : "DIV.subhead"
           },
           { "contents" : "some other content",
            "selector" : "LI:nth-child(1) A"
           }
          ],
        "page" : "about_us.php"
        }
        
        $obj = json_decode($json, true); 
        
        /* now to print contents from data */
        
        echo $obj['data']['contents']; 
         // thats all 
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-06
          • 2014-06-19
          • 2021-12-13
          • 2013-03-14
          • 2017-05-14
          • 1970-01-01
          • 2012-10-24
          • 1970-01-01
          相关资源
          最近更新 更多