Store with json reader is not working
-
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} 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} I create json using Newtonsoft.Json Dictionary<string, object> dict = new Dictionary<string, object>(); string s = JsonConvert.SerializeObject(dist); How can I remove slash in server side in order to produce valid json for extjs store. I tried result = result.Replace("\"","'"); And result =result.Replace("\"", @"""") It's not working
-
Answer:
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. See if you can return the dict on the server-side without first converting it into a string. Some code, somewhere, is taking your string and putting it in a json object with 'Results' and 'Total'. Check to see if that code can take a Dictionary as-is. 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]); } }); Then use type: 'my-json' in your reader config. Here is my test case: 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' } } });
Gregory Nozik at Stack Overflow Visit the source
Related Q & A:
- How To Download Pdf Reader?Best solution by Yahoo! Answers
- Is there any pdf reader that supports word searching with regular expression?Best solution by Super User
- How to store json data in jquery?Best solution by Stack Overflow
- How do I print Microsoft Reader (lit) file?Best solution by Yahoo! Answers
- Working at an apple store?Best solution by Ask Different
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.