使用json阅读器存储无法正常工作 [英] Store with json reader is not working

查看:112
本文介绍了使用json阅读器存储无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Sereliaze字典后创建了以下json

I have following json created after I sereliaze a dictionary

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

当我尝试将其加载到extjs存储中时,该存储未加载

When I try to load it into the extjs store , the store is not loaded

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

但是,如果我删除了硬编码的斜杠,它就可以工作了

But if I remove slash hard coded and it's working

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

我使用 Newtonsoft.Json

Dictionary<string, object> dict = new Dictionary<string, object>();

            string s = JsonConvert.SerializeObject(dist);

如何在服务器端删除斜杠,以便为extjs存储生成有效的json.

How can I remove slash in server side in order to produce valid json for extjs store.

我尝试了

result = result.Replace("\"","'");

还有

result =result.Replace("\"", @"""") 

它不起作用

推荐答案

很显然,Ext不喜欢您已将json对象编码为字符串,然后将其放在另一个json对象中.我看到了两个选择.

Clearly, Ext doesn't like that you have encoded a json object as a string and then put them in another json object. You have two options that I see.

  1. 查看是否可以在不首先将其转换为字符串的情况下在服务器端返回dict.某些地方的代码正在将您的字符串放入带有结果"和总计"的json对象中.检查该代码是否可以按原样使用Dictionary.

在客户端解开结果"属性.一种方法是创建自己的阅读器:

Unwrap the 'Results' propery on the client-side. One way would be to make your own reader:

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

然后在阅读器配置中使用type: 'my-json'.

Then use type: 'my-json' in your reader config.

这是我的测试用例:

var data = {"Results":["{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}"],"Total":4};

Ext.define('Currency', {
  extend: 'Ext.data.Model',
  fields: [
    { name: 'BaseCurrency', type: 'string' },
    { name: 'TermCurrency', type: 'string' }
  ]
});

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

var store = Ext.create('Ext.data.Store', {
  model: 'Currency',
  data: data,
  proxy: {
    type: 'memory',
    reader: {
      type: 'my-json',
      root: 'Results',
      totalProperty: 'Total'
    }
  }
});

这篇关于使用json阅读器存储无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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