Javascript Json-树数据格式

Javascript Json-树数据格式,javascript,jquery,json,jstree,Javascript,Jquery,Json,Jstree,我正在尝试格式化json resose,使其能够与 这里是url json响应 { "items": [ { "id": 1, "parent_id": 0, "name": "Root Catalog" }, { "id": 2, "parent_id": 1, "name": "Default Categ

我正在尝试格式化json resose,使其能够与

这里是url json响应

{
  "items": [
    { "id": 1, "parent_id": 0, "name": "Root Catalog" },
    { "id": 2, "parent_id": 1, "name": "Default Category" },
    { "id": 3, "parent_id": 2, "name": "category1" },
    { "id": 5, "parent_id": 3, "name": "Sub1_category1" },
    { "id": 6, "parent_id": 3, "name": "Sub2_category1" },

    { "id": 11, "parent_id": 2, "name": "category2" },

    { "id": 12, "parent_id": 2, "name": "category3" },
    { "id": 14, "parent_id": 12, "name": "Sub1_category3" },
    { "id": 15, "parent_id": 12, "name": "Sub1_category4" }
  ]
}
以下是我试图得到的回应:

[
  { 'id': '1', 'parent': '#', 'text': 'Root Catalog' },
  { 'id': '2', 'parent': '1', 'text': 'Default Category' },
  { 'id': '3', 'parent': '2', 'text': 'category1' },
  { 'id': '5', 'parent': '3', 'text': 'Sub1_category1' },
  { 'id': '6', 'parent': '3', 'text': 'Sub2_category1' },
  { 'id': '11', 'parent': '2', 'text': 'category2' },
  { 'id': '12', 'parent': '2', 'text': 'category3' },
  { 'id': '14', 'parent': '12', 'text': 'Sub1_category3' },
  { 'id': '15', 'parent': '12', 'text': 'Sub2_category3' },
]
以下是我对它的看法:

let test = {
  "items": [
    { "id": 1, "parent_id": 0, "name": "Root Catalog" },
    { "id": 2, "parent_id": 1, "name": "Default Category" },
    { "id": 3, "parent_id": 2, "name": "category1" },
    { "id": 5, "parent_id": 3, "name": "Sub1_category1" },
    { "id": 6, "parent_id": 3, "name": "Sub2_category1" },

    { "id": 11, "parent_id": 2, "name": "category2" },

    { "id": 12, "parent_id": 2, "name": "category3" },
    { "id": 14, "parent_id": 12, "name": "Sub1_category3" },
    { "id": 15, "parent_id": 12, "name": "Sub1_category4" }
  ]
}

function formatData(itemsList) {
  let formatOutput = [];
  for (item of itemsList.items) {
    if (item.parent_id > 0) {
      if (item.parent_id !== 1 && typeof formatOutput[item.parent_id] === 'undefined') {
        formatOutput[item.parent_id] = {
          "parent": item.parent_id,
          "id": item.id,
          "text": null,
          "children": [
            {
              id: item.id,
              text: item.name,
              parent: item.parent_id
            }
          ]
        }
      } else if (item.parent_id !== 1) {
        formatOutput[item.parent_id].children.push({
          id: item.id,
          text: item.name,
          parent: item.parent_id
        })
      }
    }
  }
  for (item of itemsList.items) {
    if (typeof formatOutput[item.id] === 'object') {
      formatOutput[item.id].text = item.name
    }
  }
  return formatOutput.filter(val => val)
}

console.log(formatData(test))

2个问题:

1.Json未按jstree要求格式化

  • 键“id”和“parent”的值未字符串化

  • 非常感谢您提供的任何帮助。

    我无法理解您的所有逻辑,但我希望这段代码可以帮助您

    const test={“items”:[
    {“id”:1,“parent_id”:0,“name”:“Root Catalog”},
    {“id”:2,“parent_id”:1,“name”:“Default Category”},
    {“id”:3,“parent_id”:2,“name”:“category1”},
    {“id”:5,“parent_id”:3,“name”:“Sub1_category1”},
    {“id”:6,“parent_id”:3,“name”:“Sub2_category1”},
    {“id”:11,“parent_id”:2,“name”:“category2”},
    {“id”:12,“parent_id”:2,“name”:“category3”},
    {“id”:14,“parent_id”:12,“name”:“Sub1_category3”},
    {“id”:15,“parent_id”:12,“name”:“Sub1_category4”}
    ]
    }
    const result=test.items.map(x=>{
    返回{
    id:x.id==1?#“:x.id,//如果元素的id是1 put#else x.id
    父项:x.parent\u id,
    文本:x.name
    }
    })
    
    控制台日志(结果)谢谢你的回复。它工作得很好,对我帮助很大。但是,JStree要求类别(而不是子类别)具有“父级id”:“#”。因此,我在想,是否可以在代码中添加一个if is类别“parent”:“#”else parent:x.parent_id。再次感谢您解释何时需要返回以及何时应该是parent_id?我想在数组的第一个元素处应用“parent_id”:“#”。if(/*是第一个元素*/){id:x.id,parent_id:“#”,text:x.name}else{id:x.id,parent:x.parent_id,text:x.name}嘿,我更新了代码。如果我现在得到了您想要的代码,它是正确的。尝试学习这些函数。map(),forEach(),find(),some(),filter()。它可以在很大程度上帮助您管理阵列。但是,即使如此,也不要忘记for的原语。