【问题标题】:How can I build a multidimensional array in JSON and pass it to ajax call?如何在 JSON 中构建多维数组并将其传递给 ajax 调用?
【发布时间】:2018-07-03 22:20:18
【问题描述】:

我在使用多维数组时遇到了一些问题。我的php代码如下:

$result = array();
$count = 0;

    foreach($matches_lines as $lines){

        $match_user = $lines["signup_username"];
        $match_birth = $lines["signup_birth"];
        $match_city = $lines["signup_city"];
        $match_gender = $lines["signup_gender"];
        $match_os = $lines["signup_os"];
        $match_persontype = $lines["signup_persontype"];

        if("some check on the variables"){

            $result[$count] = array('username' => "$match_user", 'birth'=> "$match_birth", 'city' => "$match_city", 'gender' => "$match_gender", 'os' => "$match_os", 'persontype' => "$match_persontype");
            $count = $count + 1;
            }
        }       
    }
    echo json_encode($result);
}

而我的 ajax 请求看起来像这样:

$("#select_age").click(function(){
        $.ajax({
            method: "POST",
            url: "get_matches.php",
            dataType: "json",
            data: {
            min_search: $( "#slider-range" ).slider( "values", 0 ),
            max_search: $( "#slider-range" ).slider( "values", 1 )
            },
            success: function(data){
                var myvar = jQuery.parseJSON(data);
                window.alert(myvar)
            }
        });
});

var_dump($result) 应该如下所示:

array(2) {
  [0]=>
  array(6) {
    ["username"]=>
    string(6) "giulia"
    ["birth"]=>
    string(10) "05/10/1990"
    ["city"]=>
    string(6) "Torino"
    ["gender"]=>
    string(1) "F"
    ["os"]=>
    string(7) "Windows"
    ["persontype"]=>
    string(4) "ENFP"
  }
  [1]=>
  array(6) {
    ["username"]=>
    string(7) "taiga27"
    ["birth"]=>
    string(10) "07/27/1998"
    ["city"]=>
    string(6) "Torino"
    ["gender"]=>
    string(1) "F"
    ["os"]=>
    string(7) "Windows"
    ["persontype"]=>
    string(4) "ISTP"
  }
}

当我到达 var myvar = jQuery.parseJSON(data);我收到错误“位置 2 的 JSON 中出现意外的令牌 a”

我做错了什么?如何在 foreach 中初始化正确的 JSON 多维数组?以及如何在ajax成功函数中检索一次数据?

【问题讨论】:

  • var_dump 没有生成 JSON 输出。改用 json_encode
  • 此外,如果数据来自数据库,则可以在 SQL 中自动构建此数组。
  • 尝试使用您的浏览器检查器/开发者工具来查看 ajax (xhr) 请求的内容。在 json 数据之前可能还有其他输出导致无法使用 parseJSON()
  • AbraCadaver 是的,我编辑了我的帖子,因为我忘记了我必须检查一些变量的条件,这就是为什么我没有使用从 SQL 到 JSON 的自动转换
  • 使用 jsonlint.com 检查 JSON 中的错误。

标签: php jquery json ajax multidimensional-array


【解决方案1】:

几点说明:

您无需增加 $count 即可追加到数组。只需排除索引(见下文)。

您不需要将变量放在双引号内。简单地传递变量,根本不带任何引号。

您的示例代码有一些额外的右括号,也许您在共享内容之前/之后还有其他代码,但如果没有,您当然不需要这么多括号。

您不应使用var_dump() 返回数据,而应使用echo() JSON 编码的结果字符串(如您在示例中所使用的那样)。

$result = array();

foreach($matches_lines as $lines){

        $match_user = $lines["signup_username"];
        $match_birth = $lines["signup_birth"];
        $match_city = $lines["signup_city"];
        $match_gender = $lines["signup_gender"];
        $match_os = $lines["signup_os"];
        $match_persontype = $lines["signup_persontype"];

        if( 1 == 1 ){ // replace with "some check on the variables"
            $result[] = array('username' => $match_user, 'birth'=> $match_birth, 'city' => $match_city, 'gender' => $match_gender, 'os' => $match_os, 'persontype' => $match_persontype);
        }
}

echo json_encode($result);

【讨论】:

  • 这样做我现在得到(查看控制台)这是一个回声 0:{用户名:“giulia”,出生:“05/10/1990”,城市:“都灵”,性别: “F”,操作系统:“Windows”,...} 1:{用户名:“taiga27”,出生:“07/27/1998”,城市:“都灵”,性别:“F”,操作系统:“Windows”,... }
猜你喜欢
  • 1970-01-01
  • 2011-07-09
  • 2018-09-05
  • 2023-03-06
  • 2015-01-16
  • 1970-01-01
  • 2019-09-12
  • 2020-11-06
  • 2012-12-03
相关资源
最近更新 更多