繁体   English   中英

Json 到 Javascript Object 阵列

[英]Json to Javascript Object Array

我正在尝试创建一个 javascript object 作为引导程序 treeview 的输入。 我有 php 从 mysql 和 json 中获取数据,将结果编码为以下结构:

{"Company 1":{"Production":["Brands","Categories","Products","Stocks"],"Sales":["Customers","Orders","Staffs","Stores"]},"Company 2":{"Production":["Brands","Categories","Products","Stocks"],"Sales":["Customers","Orders","Staffs","Stores"]}}

PHP 代码生成 json:

$databases=[];
foreach($result as $row){
$database=$row["database"];
$schema=$row["schema"];
$table=$row["object"];
if(!array_key_exists($database, $databases))
    $databases[$database]=[];
if(!array_key_exists($schema, $databases[$database]))
    $databases[$database][$schema]=[];
array_push($databases[$database][$schema], $table);
}

echo json_encode($databases);

但是我正在努力将 json 结构放入所需的 JavaScript 对象的嵌套数组中。 以下是所需的结构:

[ { text: "Company 1", nodes: [ { text: "Production", nodes: [ { text: "Brands" }, { text: "Categories" }, { text: "Products" }, { text: "Stocks" } ] }, { text: "Sales", nodes: [ { text: "Customers" }, { text: "Orders" }, { text: "Staffs" }, { text: "Stores" } ] } ] }, { text: "Company 2", nodes: [ { text: "Production", nodes: [ { text: "Brands" }, { text: "Categories" }, { text: "Products" }, { text: "Stocks" } ] }, { text: "Sales", nodes: [ { text: "Customers" }, { text: "Orders" }, { text: "Staffs" }, { text: "Stores" } ] } ] } ];

任何建议,将不胜感激

 const data = { "Company 1": { "Production": ["Brands", "Categories", "Products", "Stocks"], "Sales": ["Customers", "Orders", "Staffs", "Stores"] }, "Company 2": { "Production": ["Brands", "Categories", "Products", "Stocks"], "Sales": ["Customers", "Orders", "Staffs", "Stores"] } } function converter(data) { return Object.entries(data).reduce((converted, [key, val]) => { const element = { text: key, nodes: [...Object.entries(val).map(([key2, val2]) => { return { text: key2, nodes: [...Object.values(val2).map(val3 => { return { text: val3 } })] } })] } converted.push(element); return converted }, []); } console.log(converter(data))

我建议使用递归flatItem function 来做到这一点。 我还介绍了一个array_map_with_keys助手 function 因为 PHP 自己的array_map function 忽略键,在你的情况下,我们需要它。


function array_map_with_keys($callback, $array){
  return array_map($callback, array_keys($array), $array);
}

function flatItem($key, $value) {
  if(is_array($value)){
    return 
      ["text" => $key,
       "nodes" => array_map_with_keys("flatItem", $value)
      ];  
  }else{
    return ["text" => $value];
  }
}

$converted = array_map_with_keys("flatItem", $databases);

echo json_encode($converted);

结果是你所期望的:

[{"text":"Company 1","nodes":[{"text":"Production","nodes":[{"te
xt":"Brands"},{"text":"Categories"},{"text":"Products"},{"text":
"Stocks"}]},{"text":"Sales","nodes":[{"text":"Customers"},{"text
":"Orders"},{"text":"Staffs"},{"text":"Stores"}]}]},{"text":"Com
pany 2","nodes":[{"text":"Production","nodes":[{"text":"Brands"}
,{"text":"Categories"},{"text":"Products"},{"text":"Stocks"}]},{
"text":"Sales","nodes":[{"text":"Customers"},{"text":"Orders"},{
"text":"Staffs"},{"text":"Stores"}]}]}]

你可以把它传递给js。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM