PDA

View Full Version : converting a JSON field into a variable



gr0uch0
8 Nov 2010, 1:16 PM
Hi,
I'm new to EXTJS, so please don't flame what I think is probably a very simple question.

I'm trying to convert a field in a JSON array into a variable. What I'm trying to do in the code below is query the DB and extract the firstname from the array to display on the toolbar (although I've just made it a messagebox for now). The gameplan is use the variable outside the scope of the JSONSTORE so it can be used without overnesting the "SUCCESS".

Any help much appreciated


Ext.namespace('arc');

//create application
arc.app = function() {
return {
init : function() {

var priv = new Ext.data.JsonStore({
url:'login/privs.php'
,id:'privstore'
,root:'data'
,storeId: 'privdata'
,autoLoad:true
,fields:[{name:'firstname'}]
});


var jsonData = Ext.util.JSON.decode(priv);
var sss = jsonData.data[0].firstname;


Ext.Msg.alert('username',sss);

}
}
}();

Ext.onReady(arc.app.init, arc.app);




this is the returned array from the php call

{success: true, data: [{"firstname":"Charles","lastname":"Smith","home":"1","transport":"1","maintenance":"1","address_book":"0","event_planner":"0","admin":"0"}]}

Animal
8 Nov 2010, 2:39 PM
autoLoad: {
callback: fnWhichProcessesStore'sData
}


And the Store is an object, not a JSON String. It's a.... well, it's a Store! Containing Records. Which are made up of Fields.

What do you want to DO with these records in that callback?

gr0uch0
8 Nov 2010, 3:07 PM
Thanks for that Animal - I told you it was an easy one :-)

What I'm aiming at doing is using the fields in the 1 record store to determine what menu buttons and access levels each logged in user is allowed to see/use. So in the example:

firstname -> printed on the top toolbar (e.g. Welcome: Charles)
home/maintenance/address_book etc. fields -> a 0 indicates that the menu button is hidden, a 1 indicates that the button is shown and the details for the 'button-function' are read only and a 2 has full edit access.

delvia_bles
30 Dec 2010, 12:30 AM
Hi can you please tell how to extract 'fields' from json data?? I need them for displaying chart.


cpuStore.load();
var fields = cpuStore.reader.meta.fields;//this.store.reader.meta.fields;//


var series = [];
for(var i=1;i<fields.length;i++){
// console.log(i);
var newSeries = new Ext.chart.LineSeries({
type: 'line',
displayName: fields[i].name,
yField: fields[i].name,
xField: fields[0],
style: {
mode: 'stretch',
color: this.chartColors[i+1]
}
});
series.push(newSeries);
}
cpuChart.series = series;

Here cpuStore is JsonStore and reader property has DataReader and json data is getting loaded before function is getting called. I have written above code on button click but its giving me error in that 'fields' is undefined in for loop. Plz help me!

slemmon
30 Dec 2010, 10:58 AM
Are you needing a list of all of the keys?
Perhaps decode the JSON: o = Ext.util.JSON.decode(JSON_STRING) > object
Then Ext.iterate to read out the keys.



Ext.each(o, function(key, value, obj){
alert(key)
})

delvia_bles
5 Jan 2011, 1:06 AM
I dont understand the decode JSON step. Can you please elaborate. I am quite new to Extjs.