创建自定义JSON阅读器 [英] Create custom json reader

查看:115
本文介绍了创建自定义JSON阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据詹姆斯·克拉克(James Clark)在随后的帖子中创建的自定义json阅读器. /a>

I created custom json reader as suggest by James Clark in following post.

这是用于创建自定义json的代码

Here is a code for creating custom json

  Ext.define('MyReader', {
            extend: 'Ext.data.reader.Json',
            alias: 'my-json',
            read: function (object) {
                debugger;
                object.Results = Ext.decode(object.responseText);
                this.callParent([object]);
            }
        });

在商店定义中,我会调用自定义阅读器

In store definition I assing custom reader

var store = Ext.create('Ext.data.Store', {
//model: 'Option',
    fields: fields,
    pageSize: itemsPerPage,
    proxy: {
        type: 'ajax',
        url: getDataWithPageURL,
        **reader:Ext.create('MyReader', {root: 'Results', totalProperty: 'Total'})**
    }
});

我从客户端收到的json如下

The json that I receiving from clients look like this

{"Results":["{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}"],"Total":4}

我在ext-js.js中收到错误 我调试了它,并且错误发生在Ext.data.proxy.Server类中 以下代码

I receiving an error in ext-js.js I debuged it and the error occurs in Ext.data.proxy.Server class in following code

if (success === true) {
        reader = me.getReader();
        result = reader.read(me.extractResponseData(response));
        records = result.records;

结果未定义.

请帮助

推荐答案

我初次尝试时出现的代码问题是:

The problems with the code in my initial attempt were:

  1. read()方法应返回一个值,因此应显示:

return this.callParent([object]);

  1. 别名应该是'reader.my-json'

需要映射结果,因为它是一个数组:

The results needed to be mapped because it was an array:

object.Results = Ext.Array.map(object.Results, Ext.decode);

固定这些内容后,商店可以使用更简单的阅读器定义:

With those fixed, the store can use the simpler reader definition:

reader: {
  type: 'my-json',
  root: 'Results',
  totalProperty: 'Total'
}

但是请参阅原始问题中的完整测试用例以获取完整代码.对于尚未完全测试最初提出的代码,我深表歉意.

But see the complete test case in the original question for the full code. I apologize for not having thoroughly tested the code I initially proposed.

这篇关于创建自定义JSON阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆