阅读JSON与关键:在ext.store价值煎茶触摸2关键、价值、JSON、store

由网友(毛主席夸涐帅)分享简介:我试图加载特定的JSON文件到列表视图;但煎茶代理似乎并不了解的关键对CurrencyName和价值。我无言以对如何这两个值相关联成可用的数据。下面是JSON的:{时间戳:1335294053,基地:美元,利率:{AED:3.6732,AFN:48.32,ALL:106.040001}}我的商店:代理:{类型:阿贾克...

我试图加载特定的JSON文件到列表视图;但煎茶代理似乎并不了解的关键对CurrencyName和价值。我无言以对如何这两个值相关联成可用的数据。

下面是JSON的:

  {
    时间戳:1335294053,
    基地:美元,
    利率:{
        AED:3.6732,
        AFN:48.32,
        ALL:106.040001
    }
}
 

我的商店:

 代理:{
    类型:阿贾克斯,
    网址:HTTP://localhost/CurrencyFX/latest.json,
    读者: {
        类型:JSON,
        rootProperty:'率'
    }
},
 
Qt中的JSON操作 3 JSON在Qt中的应用举例 写入和读出的关键是利用QJsonDocument实现Json格式的文件转换为字符串

我的模型:

  Ext.define(CurrencyFX.model.Currency',{
    延伸:Ext.data.Model,
    配置:{
        田:['名','值']
    }
});
 

解决方案

您需要编写自己的JSON的读者子类来完成这项工作,因为你正在处理的数据是不是数组。

值得庆幸的是,这样做是相当简单的。下面是一些应该做的工作:

  Ext.define(Ext.data.reader.Custom',{
    延伸:Ext.data.reader.Json,
    别名:reader.custom,

    getRoot:功能(数据){
        如果(this.rootAccessor){
            数据= this.rootAccessor.call(这个数据);
        }

        变种值= [],
            名称;

        对于(数据名称){
            values​​.push({
                名称:名称,
                值:数据[名]
            });
        }

        返回值;
    }
});
 

这将与下面的存储配置:

  VAR店= Ex​​t.create('Ext.data.Store',{
    田:['名','值'],
    自动加载:真,
    代理: {
        类型:阿贾克斯,
        网址:0000.json,
        读者: {
            类型:自定义,
            rootProperty:'率'
        }
    }
});
 

请注意阅读键入现在自定义

我测试了在本地与您的数据,它似乎工作得很好。

I'm trying to load a particular json file into a listview; but sencha proxy doesn't seem to understand the key pairs of "CurrencyName" and "Value". I'm clueless on how to associate those two values into usable data.

Here's the json:

{
    "timestamp": 1335294053,
    "base": "USD",
    "rates": {
        "AED": 3.6732,
        "AFN": 48.32,
        "ALL": 106.040001
    }
}

my store:

proxy: {
    type: 'ajax',
    url: 'http://localhost/CurrencyFX/latest.json',
    reader: {
        type: 'json',
        rootProperty: 'rates'
    }
},

my model:

Ext.define('CurrencyFX.model.Currency', {
    extend: 'Ext.data.Model',
    config: {
        fields: [ 'name', 'value' ]
    }
});

解决方案

You'll need to write your own JSON reader subclass to make this work, as the data you are dealing with isn't an array.

Thankfully, doing this is fairly simple. Here is something that should do the job:

Ext.define('Ext.data.reader.Custom', {
    extend: 'Ext.data.reader.Json',
    alias : 'reader.custom',

    getRoot: function(data) {
        if (this.rootAccessor) {
            data = this.rootAccessor.call(this, data);
        }

        var values = [],
            name;

        for (name in data) {
            values.push({
                name: name,
                value: data[name]
            });
        }

        return values;
    }
});

Which will work with the following store configuration:

var store = Ext.create('Ext.data.Store', {
    fields: ['name', 'value'],
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: '0000.json',
        reader: {
            type: 'custom',
            rootProperty: 'rates'
        }
    }
});

Notice the reader type is now custom.

I tested this locally with your data and it seemed to work just fine.

阅读全文

相关推荐

最新文章