PDA

View Full Version : Loading a JSON without field name assigned to it



thangamalar
15 Jul 2010, 10:54 PM
Hi, in my project, I need to load the following JSON Array in a ComboBox.

{"regions": [ "World", "NorthAmerica", "EMEA", "Asia", "MetroNY", "NY-CDC", "NY-BDC", "NY-30H", "NY-Secaucus", "NY-Piscataway", "ETC", "MetroLondon", "LDN-RC", "LDN-CCC", "LDN-DC2", "MetroTokyo", "TKY-DC1", "TKY-DC2", "TKY-DC3", "MetroHK", "HK-DC1", "HK-DC2", "CDC-RP", "BDC-RP" ]}


Since there are no fields assigned with anyvalue there in the whole string array, how do I retrieve it?

As of now, I tried without fields in the JsonStore like below but no help as the store.getCount() returns me 0 always.


var regionStore = new Ext.data.JsonStore({
root:'regions'
,url:'/possum/gui/query/listRegions.jsp'
,autoLoad:true
/*,fields:[
{name:'subnet', mapping:'network'}
,{name:'profile', mapping:'hvProfile'}
,{name:'region', mapping:'region'}
,{name:'vlans', mapping:'vlans[0].network'}
,{name:'netmask', mapping:'netmask'}
]*/
});

alert (regionStore.getCount());

Animal
15 Jul 2010, 11:38 PM
mapping: 'network'

You have read what the mapping config is of course before writing your code? You wouldn't copy/paste code from somewhere and use that as a template from then on would you?

thangamalar
16 Jul 2010, 12:12 AM
Animal,

I have commented out the 'fields' man in my post. The reason for showing that is how do I do it when there is no mapping field associated in JSON. The JSON is plain and just an Array. I just have the root field (which I can remove if needed from my JSP) and the values in the JSON and I need the values to be populated in my ComboBox. Hope it makes clear.

Condor
16 Jul 2010, 12:45 AM
Use:

{field: 'region', mapping: function(v){return v;}}

Animal
16 Jul 2010, 12:52 AM
I don't like these shortcut classes because they hide reality from developers who won't read docs (hence this case where you use JsonStore without understanding).

But can use an ArrayStore.

In reality, you need to create en Ext.data.Store configured with an Ext.data.ArrayReader

thangamalar
16 Jul 2010, 3:20 AM
Basically I go through all the documentation and try use the APIs available. Frankly I have used the ArrayStore initially which was reading from a canned ArrayStore. Then my colleague have made an API which was returning String[] at the server side for the same Regions list and then I had this trouble.

Since it's just 2 months of usage, I am getting to know better though not at the very best level. So sorry about my silly questions if it's not making clear to people.

The documentation of ExtJS is not so clear basically for me and in general many have the same feeling here in our office. I will use the ArrayReader and update you. Thanks.

Condor
16 Jul 2010, 3:57 AM
You can't use an ArrayReader, because:
1. The data has a root and ArrayReader didn't support a root before Ext 3.2.
2. The array value is the field and not the record, so you need a mapping or convert function, because JsonReader doesn't support this by default.

Animal
16 Jul 2010, 4:46 AM
ArrayReader does support root:




Ext.data.ArrayReader = Ext.extend(Ext.data.JsonReader, {
/**
* @cfg {String} successProperty
* @hide
*/
/**
* @cfg {Number} id (optional) The subscript within row Array that provides an ID for the Record.
* Deprecated. Use {@link #idIndex} instead.
*/
/**
* @cfg {Number} idIndex (optional) The subscript within row Array that provides an ID for the Record.
*/
/**
* Create a data block containing Ext.data.Records from an Array.
* @param {Object} o An Array of row objects which represents the dataset.
* @return {Object} data A data block which is used by an Ext.data.Store object as
* a cache of Ext.data.Records.
*/
readRecords : function(o){
this.arrayData = o;
var s = this.meta,
sid = s ? Ext.num(s.idIndex, s.id) : null,
recordType = this.recordType,
fields = recordType.prototype.fields,
records = [],
success = true,
v;

var root = this.getRoot(o);