【问题标题】:How to parse a response from extjs proxy?如何解析来自 extjs 代理的响应?
【发布时间】:2016-09-29 14:20:05
【问题描述】:

我收到一个 json 对象作为来自 web 服务的响应,其格式类似于:-

      {
         studentData:[
            {
                "history":25,
                 "science":69,
                 "maths":45
            }
           ]
      }

我正在使用以下代码来读取 JSON:-

Ext.define('MyStore.store.dashboard.graphs.Temp', {
extend: 'Ext.data.Store',
 proxy: {
        type: 'ajax',
        url: 'abc.php', 

        headers: {
            'Content-Type': 'application/json'
        },

         reader: {
                type: 'json',
                rootProperty: 'studentData',

                listeners: {

                    exception: function(reader, response, error, eOpts) {
                        console.log('YT reader exception');
                        console.log(error.message, error);
                        console.log(response);
                    }

                }
            }

我可以解析 JSON 响应,使其以以下格式存储吗?

     {
         studentData:[
            {
                "subject":"History",
                 "marks":"25"    
            },
           {
                 "subject":"Maths",
                 "marks":"25"
           }
           ]
      }

我需要在以 xfield 作为主题并以 yfield 作为标记的图表中显示这些值。 是否有我可以附加到代理的监听器,以便我可以根据我的要求修改存储结构?

【问题讨论】:

标签: javascript json extjs extjs-chart


【解决方案1】:

根据您使用的 ExtJS 版本,您可以在商店加载之前更改 JSON 数据。

ExtJS 4 - 您可以通过扩展 Ext.data.reader.Reader 创建自己的阅读器类 类并覆盖 getResponseData(response) 并更改 json 并返回 ResultSet。

ExtJS 5 - 您可以使用 Reader 类的转换功能,并可以更改响应数据对象。

    Ext.create('Ext.data.Store', {
    model: 'User',
    proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json',
            transform: {
                fn: function(data) {
                    // do some manipulation of the raw data object
                    return data;
                },
        scope: this
            }
        }
    },});

【讨论】:

    【解决方案2】:

    您可以在阅读器中使用transform 函数:

    如果设置了转换函数,它将在之前调用 readRecords 执行。它传递原始(反序列化)数据对象。 transform函数返回一个数据对象,可以修改 原始数据对象的版本,或全新的数据对象。 转换可以是函数,也可以是 Reader 上的方法名称 实例,或具有“fn”键和可选“范围”键的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 2016-05-29
      • 2012-10-09
      相关资源
      最近更新 更多