Results 1 to 6 of 6

Thread: converting a JSON field into a variable

  1. #1
    Sencha User
    Join Date
    Sep 2010
    Posts
    19
    Vote Rating
    3
      0  

    Default converting a JSON field into a variable

    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

    Code:
    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);
    Code:
    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"}]}

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890
    Vote Rating
    90
      0  

    Default

    Code:
    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?

  3. #3
    Sencha User
    Join Date
    Sep 2010
    Posts
    19
    Vote Rating
    3
      0  

    Default

    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.

  4. #4
    Sencha User
    Join Date
    Dec 2010
    Posts
    35
    Vote Rating
    0
      0  

    Default

    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!

  5. #5
    Sencha User slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    6,165
    Vote Rating
    255
      0  

    Default

    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.
    Code:
     
     
    Ext.each(o, function(key, value, obj){
        alert(key)
    })

  6. #6
    Sencha User
    Join Date
    Dec 2010
    Posts
    35
    Vote Rating
    0
      0  

    Default

    I dont understand the decode JSON step. Can you please elaborate. I am quite new to Extjs.

Similar Threads

  1. Converting Record.data to JSON for server side submission
    By techakone in forum Ext 3.x: Help & Discussion
    Replies: 5
    Last Post: 30 Jan 2011, 10:38 PM
  2. Converting json to java object using struts json plugin
    By riya_phoenix in forum Ext 3.x: Help & Discussion
    Replies: 0
    Last Post: 16 Dec 2009, 5:08 PM
  3. converting json object to xml
    By bhanu prakash dasari in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 18 Mar 2009, 11:39 PM
  4. Converting JSON Data to a Record
    By Vikram1507 in forum Ext 2.x: Help & Discussion
    Replies: 7
    Last Post: 18 Feb 2009, 2:35 AM
  5. converting xml to json
    By antimatter15 in forum Ext 1.x: Help & Discussion
    Replies: 2
    Last Post: 30 Sep 2007, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •