extJS:读取嵌套的 JSON

发布于 2024-10-31 10:57:39 字数 1710 浏览 2 评论 0原文

我有一个来自 ldap_search() 调用的漂亮嵌套 JSON。我想使用这些信息来填充 ExtJS ComboBox,但我在读者方面遇到了一些麻烦。显然,我无法在 ComboBox 中读取我需要的信息,即人员的邮件地址、uid 和 cn

我认为整个问题在于商店。我正在尝试以下代码:

var store= new Ext.data.JsonStore({
        url:'search.php',   
        root: '',
        totalProperty: 'count',
        fields: [
            {name:'cn', type: 'string', mapping:'cn.0'},
            {name:'mail', type: 'string', mapping:'mail.0'},
            {name:'uid', type: 'string', mapping:'uid.0'}
        ]
});

但 FireBug 告诉我 missing ; before 语句在 ext-all.js 中返回 obj.cn.0(第 7 行)。我尝试了另一个更简单的 JSON 数组,它可以工作,这就是为什么我真的认为问题出在这部分代码中,尤其是在映射中。

search.php 返回的 JSON 示例是:

{
  "count": 2,
  "0": {
    "mail": {
      "count": 1,
      "0": "Mail address not registered."
    },
    "0": "mail",
    "uid": {
      "count": 1,
      "0": "[email protected]"
    },
    "1": "uid",
    "cn": {
      "count": 1,
      "0": "Surname0 Name0"
    },
    "2": "cn",
    "count": 3,
    "dn": "cn=Surname0 Name0,ou=personal,dc=domain,dc=com"
  },
  "1": {
    "mail": {
      "count": 1,
      "0": "[email protected]"
    },
    "0": "mail",
    "uid": {
      "count": 1,
      "0": "name1.surname1"
    },
    "1": "uid",
    "cn": {
      "count": 1,
      "0": "Surname 1 Name 1"
    },
    "2": "cn",
    "count": 3,
    "dn": "cn=Surname1 Name1,ou=personal,dc=domain,dc=com"
  }
}

感谢您的宝贵时间。

I have a pretty nested JSON coming from a ldap_search() call. I would like to use this information to populate an ExtJS ComboBox, but I am facing some troubles with the reader. Apparently, I am not able to read the information that I need in the ComboBox, that is the mail address of the people, the uid and the cn

I think the whole problem lies in the store. I was trying the following code:

var store= new Ext.data.JsonStore({
        url:'search.php',   
        root: '',
        totalProperty: 'count',
        fields: [
            {name:'cn', type: 'string', mapping:'cn.0'},
            {name:'mail', type: 'string', mapping:'mail.0'},
            {name:'uid', type: 'string', mapping:'uid.0'}
        ]
});

but FireBug told me missing ; before statement return obj.cn.0 in ext-all.js (line 7). I tried with another, easier JSON array and it works, that is why I really think the problem lies in this part of code, especially in the mapping.

an example of JSON returned by search.php is:

{
  "count": 2,
  "0": {
    "mail": {
      "count": 1,
      "0": "Mail address not registered."
    },
    "0": "mail",
    "uid": {
      "count": 1,
      "0": "[email protected]"
    },
    "1": "uid",
    "cn": {
      "count": 1,
      "0": "Surname0 Name0"
    },
    "2": "cn",
    "count": 3,
    "dn": "cn=Surname0 Name0,ou=personal,dc=domain,dc=com"
  },
  "1": {
    "mail": {
      "count": 1,
      "0": "[email protected]"
    },
    "0": "mail",
    "uid": {
      "count": 1,
      "0": "name1.surname1"
    },
    "1": "uid",
    "cn": {
      "count": 1,
      "0": "Surname 1 Name 1"
    },
    "2": "cn",
    "count": 3,
    "dn": "cn=Surname1 Name1,ou=personal,dc=domain,dc=com"
  }
}

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-11-07 10:57:39

是的,该 JSON 结构不会立即与标准 ExtJS JSONReader 一起使用。看一下取自 ExtJS API 文档 JSON 应该是什么样子。

{
    results: 2000, // Reader's configured totalProperty
    rows: [        // Reader's configured root
        // record data objects:
        { id: 1, firstname: 'Bill', occupation: 'Gardener' },
        { id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
        ...
    ]
}

另外,root 配置选项是必需的,不能将其留空。在上面的示例中,您的root 将是“rows”。

您可能需要首先将 JSON 解析为更简单的格式,然后再将其提供给 JSONReader。

Yep, that JSON structure is not going to work straight away with standard ExtJS JSONReader. Take a look at this example taken from the ExtJS API documentation on how the JSON should look like.

{
    results: 2000, // Reader's configured totalProperty
    rows: [        // Reader's configured root
        // record data objects:
        { id: 1, firstname: 'Bill', occupation: 'Gardener' },
        { id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
        ...
    ]
}

Also, the root config option is required, you cannot leave it empty. In the above example your root would be "rows".

You are probably going to need to parse that JSON of yours into a simpler format at first, before feeding it to the JSONReader.

仄言 2024-11-07 10:57:39

我想做同样的事情,但让其中一个嵌套项目成为我图表中的一个字段。这篇文章不断出现,所以我认为看看我为解决图表问题所做的事情可能会有所帮助。解决这个问题的关键是知道标签配置存在: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.chart.Label。使用它,您可以覆盖传入内容的默认渲染。在本例中,字段是“key”(此处未显示,但我的模型使用“key”的默认类型(即,不是字符串))。关键对象被传递给渲染器。使用 function(t),我现在可以像 JavaScript 一样访问该对象并传回该对象下的名称。

 json
    key : { 
            wholePath : "c:/.../fileName.txt",
            fileName : "fileName.txt",
        }
  code:
    axes: [
    {
        title: 'Values',
        type: 'Numeric',
        position: 'left',
        fields: ['value'],
        minimum: 0,
        maximum: 100,
        minorTickSteps: 1
    },
    {
        title: 'File Name',
        type: 'Category',
        position: 'bottom',
        fields: ['key'],
        label: {
            renderer: function(t) {
               var fileName = t.name;
               return fileName;
            }
        }
    }

I was looking to do the same thing, but have one of the nested items be a field in my chart. This post kept coming up, so I thought it might be helpful to see what I did to solve the chart issue. The key to solving it is knowing that the label config exists: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.chart.Label. Using that you can override the default render of what you pass in. In this example the field is "key" (Not shown here, but my model is using the default type for 'key' (ie., not string)). The key object gets passed to renderer. Using function(t), I can now access that object like javascript and pass back the name under the object.

 json
    key : { 
            wholePath : "c:/.../fileName.txt",
            fileName : "fileName.txt",
        }
  code:
    axes: [
    {
        title: 'Values',
        type: 'Numeric',
        position: 'left',
        fields: ['value'],
        minimum: 0,
        maximum: 100,
        minorTickSteps: 1
    },
    {
        title: 'File Name',
        type: 'Category',
        position: 'bottom',
        fields: ['key'],
        label: {
            renderer: function(t) {
               var fileName = t.name;
               return fileName;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文