将自定义JSON解析为ExtJS 4.1 TreeStore

时间:2012-07-06 11:02:23

标签: extjs

我有一个HTTP服务器,它使用JSON字符串进行响应。它们看起来像这样:

{
  ...
  "value":{
    "children":[
      {
        "path":"KEY_518693",
        "name":"KEY_518693",
        "children_count_overall":0,
        "children_page":1,
        "children_pages":1,
        "children_pagesize":10
        ...
      }
    ],
    "children_count_overall":1,
    "children_page":1,
    "children_pages":1,
    "children_pagesize":1,
    "name":null,
    "path":null,
    ...
  }
}

每个请求都有父节点(在"value"中),我请求子节点和这些子节点(在"children"中),也有一些分页信息。

如果未指定父节点,则它将根节点作为虚拟根节点的子节点返回。

我将代理的"root"属性设置为"value",因此代理知道在哪里搜索节点,但是我的TreeStore的tree成员在之后有一个奇怪的结构load()

tree: {
  childNodes: [
    {
      childNodes: [], <-- no child?
      data: {
        children: [
          { ...the raw data of the "children" node (like in the example above)...}
        ],
        ... the data of the "value" node and some data from the implicit node model...
      }
    }
  ]
}

以某种方式,"value"中的对象被转换为一个节点并插入到树中,但"children"数组中的对象将不会被转换并最终作为此{{1}的数据}节点。

1 个答案:

答案 0 :(得分:1)

TreeStore旨在使用这种类型的JSON对象:

{
  "success": true, 
  "Root": [
          {...},{...},{...} //Children
          ]
}

由于您没有在Root节点中指定数组,因此所有这些信息都将作为Raw数据传递。此外,如果您希望能够访问数据而无需查看“原始”信息,最好指定一个模型,您可以在其中指定每个节点数据中的哪些字段。

所以基本上我会说重做服务器响应或自己查看原始数据会更好。