Rick Weller
Rick Weller

Reputation: 1258

load data from json into extjs

i want to load one value from a json into a var in an extjs app. The json output looks like this

{"rechten":[{"name":"baas"}]}

If i want to use it in a store i know what to do but i use that only for grids before. I want a var like this

 var rechten = "baas";

so i can check this into something else:

if(rechten == "baas") { alert('je bent de baas') } 
else { alert('helaas je bent arbeider')};

so my question is how can i check the name value from that json in this if else statement?

Upvotes: 0

Views: 3544

Answers (3)

Donatas Olsevičius
Donatas Olsevičius

Reputation: 1350

Ext.Ajax.request({
   url: 'your_url_here',
   success: function(response, opts) {
      var o = Ext.decode(response.responseText);
      alert(o.rechten[0].name); // alerts "baas"
   },
   failure: function(response, opts) {
      console.log('server-side failure with status code ' + response.status);
   }
});

Upvotes: 1

Jan Korenek
Jan Korenek

Reputation: 214

You can load your data into a store (eg. store.load()) and then check for the value via standard store methods (eg. store.getAt()).

Or you can use Ext.Ajax and Ext.JSON it will look like this

Ext.Ajax.request({
url: 'someurl',
    success: function(response){
         var decoded = Ext.JSON.decode(response.responseText);
         console.log(decoded.rechten.name);
    }
});  

Second method will skip required model/store declaration and will be out of EXTJS MVC data concept, but for quick and easy data processing should be fine to use.

Upvotes: 2

John Rice
John Rice

Reputation: 1747

If your json string were to come from a file named page-with-rechten.xyz this snippet could call that file and then decode the text creating a JavaScript object you could then use properties of.

You can inspect the rechten object in the console and then add the additional logic you

Ext.Ajax.request({
    url: './page-with-rechten.xyz',
    success: function(response){
        var text,
            rechten;
        text = response.responseText;
        console.log('response text: ', text);
        rechten = Ext.decode(text);
        console.log('rechten object: ', rechten);        
    }
});

Upvotes: 1

Related Questions