在javascript中检索多维json数组

在javascript中检索多维json数组,javascript,json,Javascript,Json,我知道在这个话题上有很多问题和答案,但没有一个能回答我的具体问题——我已经搜索和转圈一个月了 我使用php从may数据库获取值,并通过json返回: $staff_list = array( "name" => $staff_names, "id" => $staff_ids, "img" => $staff_imgs, "typeID" => $staff_types, );

我知道在这个话题上有很多问题和答案,但没有一个能回答我的具体问题——我已经搜索和转圈一个月了

我使用php从may数据库获取值,并通过json返回:

    $staff_list = array(
          "name" => $staff_names,
          "id" => $staff_ids,
          "img" => $staff_imgs,
          "typeID" => $staff_types,
    );

    print(json_encode($staff_list));
我正在使用javascript:

$.getJSON(requestURL, function(data) {
    if( data.errorResponse ) {
            element.html("<p>(" + data.errorResponse.message + ")</p>");
    } else {
        $('#designeesloading').remove();

        $.each(data, function(i, field){
            $.each(field, function(x, value){
                haystack.push({
                  i:value //this should put into 4 arrays as per above shouldn't it?
                });
            });
        });


    } //errorResponse else

  }); //getJSON
如果有人想看,就在这里。令人沮丧的是,我会在5分钟内用PHP完成这项工作


提前感谢您的帮助。

您的代码有两个问题。首先,您只是将所有内容推到了
干草堆上
,而不是创建嵌套对象来容纳每个员工。第二,在像
{key:val}
这样的对象文本中,
i
不被计算,而是被当作一个文本名称;仅计算
val
。如果要使用计算键,必须使用方括号表示法

    $.each(data, function(i, field){
        $.each(field, function(x, value){
            if (!haystack[x]) {
                haystack[x] = {};
            }
            haystack[x][i] = value;
        });
    });

您正在将所有内容推送到
haystack
数组的顶层,而不是推送到子元素。我试图找到您在网站上指定的JS代码,但找不到。你能提供一个JSFIDLE吗?
    $.each(data, function(i, field){
        $.each(field, function(x, value){
            if (!haystack[x]) {
                haystack[x] = {};
            }
            haystack[x][i] = value;
        });
    });