【问题标题】:How to convert multi dimensional json to php array?如何将多维json转换为php数组?
【发布时间】:2020-01-30 09:21:02
【问题描述】:

我被卡住了,我已经在堆栈上搜索并搜索了这里。

首先,我创建一个 JS obj:

var formData = {};

//LOOP THREW TABLE ROWS
$("tr.element_row").each(function(index, element){

     var $this = $(this);
     var $inputs = $this.find("input.formData");

     formData[index] = {};

      //LOOP THREW INPUTS
      $.each($inputs, function(n, e){
         //this is each input in this tr

         if( $(this).attr('name') == 'el' ){
             formData[index]['el'] = $(this).val();
         } ...

然后我用 JSON stringify 转换它:

var myJSON = JSON.stringify(formData);

//RESULT (console.log(myJSON))
{"0":{"obj":"1234","el":"1","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"1":{"obj":"1234","el":"2","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"2":....

然后我用 ajax 将它发送到 PHP:

$.ajax({
    url: 'php/add_elementdata.php',
    method: 'post',
    dataType: "json",
    data: myJSON,

然后我做一个var_dump($_POST)

//RESULT
array(1) {
["{"0": {"obj":"1234","el":"1","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"1":{"obj":"1234","el":"2","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"2":{"obj":"1234","el":"3","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"3":{"obj":"1234","el":"4","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"},"4":{"obj":"1234","el":"5","lit":"1","height":"","type":"","length":"","width":"","weight_kg":"","proj":"BC"}}"]=> string(0) ""}

那我想创建一个这个..array..string..的多维数组。

$arr = json_decode($_POST);
or
$arr = json_decode($_POST, true);
echo $arr;

但是$arr 返回空。为什么?

【问题讨论】:

  • var_dump($arr); 不回显它的标准对象
  • @Rahul 好吧,如果它成功了,它有一个悲伤的“数组”,但现在 echo 什么也没说,而 var_dump 说“null”
  • 尝试使用 data: {'myJSON': myJSON} 代替 data: myJSON。并在服务器端通过 print_r($_POST) 打印它
  • $_POST 返回所有帖子变量的数组。尝试json_decode($_POST[0]); 只获取第一个数组。
  • 你也应该发送data: {'myJSON':myJSON}然后用$_POST['myJSON']获取它

标签: php arrays json string


【解决方案1】:

你传递了data: myJSON,,这意味着你传递了空值的变量(你的JSON字符串)。它显示在var_dump() - ["json" => '']。所以,你可以通过下面的方式在 PHP 中使用它:

 foreach($_POST as $data=>$emptyVal){
   // here you can add any checking stuff
    $arr = json_decode($data,true) ;
 }

print_r($arr);

Example

但是,您只需将data: myJSON, 更改为例如data: {jsonn : myJSON},。现在您将能够在 PHP 中以 $_POST['jsonn'] 的形式使用它:

$arr = json_decode($_POST['jsonn'],true) ;

【讨论】:

    【解决方案2】:

    当您通过 jQuery 发送 ajax 请求并且想要发送 FormData 时,您不需要在此 FormData 上使用 JSON.stringify。

    当您发送文件时,内容类型必须是多部分/表单数据。 所以使用这样的东西

    $.ajax({
        type: "POST",
        url: "url",
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
            console.log(response);
        },
        error: function(errResponse) {
            console.log(errResponse);
        }
    });
    

    【讨论】:

    • 在我使用创建字符串的解决方案之前,我正在发送一个对象。但它没有用。该对象在 111 个嵌套索引后被剪切.. 我发送 199.. 但只有 111 个被接收到 php 文件
    • 您是否尝试过序列化表单数据?
    • 我也想知道JSON.stringify()是否必要,但是1)OP没有使用FormData; 2) OP 没有发送文件。 3)contentType: false? multipart/form-data 推荐怎么了?
    • 设置processData为false(防止jQuery自动将数据转换为查询字符串),设置contentType为false(需要设置,否则jQuery会设置错误)。
    【解决方案3】:

    试试

    $.ajax({ type: "POST", url: "url", data: {data: myJSON}, });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 2015-01-06
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      相关资源
      最近更新 更多