cancel
Showing results for 
Search instead for 
Did you mean: 

How to read JSON data which is coming from server and store in local variable in SAP UI5?

Former Member
0 Kudos

Hello Experts,

I am not able to read the JSON data which is coming from server and I would like to store one value into local variable? It is working fine if I bind the same model to UI controller ,the data is displaying correctly.But I need store one value into local variable.

Here is my code:

var oModelData = new sap.ui.model.json.JSONModel();

    var root = window.location.protocol + '//' + window.location.host;

    var url = root + "/idmrest/v72alpha/entries/0";

    oModelData.loadData(url);

    this.getView().setModel(oModelData, "model1");

   

    var zkey = this.getView().getModel("model1").getProperty("/ENTRIES/MSKEYVALUE/VALUE");

and It is saying "undefined" and if I use:

var zkey = oModelData.getProperty("/ENTRIES/MSKEYVALUE/VALUE");

here also it is undefined

can any body please suggest me here? and also how do we get the array and loop it which is inside the JSON (model1)?

Regards,

Kiran

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos

oModelData.attachRequestCompleted(function() {

var zkey = oModelData.getProperty("/ENTRIES/MSKEYVALUE/VALUE");

  });

Former Member
0 Kudos

Hello ,

Thanks for your help. Now I can get the value.But still I am not able to make avialable that variable out of method

attachRequestCompleted(). can you please let me know how can I make this variable global..


Regards,

Kiran

former_member182372
Active Contributor
0 Kudos

call loadData like


oModelData.loadData(url, null, false);

JSONModel.prototype.loadData = function(sURL, oParameters, bAsync, sType, bMerge, bCache, mHeaders){
Former Member
0 Kudos

Hello ,

Thanks.

can you please explain me bit more. how can I do this in my case?

var oModelData = new sap.ui.model.json.JSONModel();

    var root = window.location.protocol + '//' + window.location.host;

    var url = root + "/idmrest/v72alpha/entries/0";

    oModelData.loadData(url);

    this.getView().setModel(oModelData, "model1");

oModelData.attachRequestCompleted(function() {

var zkey = oModelData.getProperty("/ENTRIES/MSKEYVALUE/VALUE");

  });

Now I need to get Zkey as available globally..

regards,

Kiran

former_member182372
Active Contributor
0 Kudos

third param is asunc - whether call is performed sync or async. bydefault it is async. if you dont like attachRequestCompleted you can call  it as sync and wait till call is done

Former Member
0 Kudos

Hello Maskim,

Here is what I tried ,but still no luck..

    oModelData.loadData(url,null,false);

      var zkey;

    oModelData.attachRequestCompleted(function() {

    zkey = oModelData.getProperty("/ENTRIES/Z_MSKEY/VALUE");

    });

   alert(zkey);

Regards,

Kiran

former_member182372
Active Contributor
0 Kudos

for sync you dont need attachRequestCompleted

oModelData.loadData(url,null,false);

      var     zkey = oModelData.getProperty("/ENTRIES/Z_MSKEY/VALUE");

   alert(zkey);

former_member182372
Active Contributor
0 Kudos

really, dont use sync just because you cant implement call back method

Former Member
0 Kudos

Hello Maskim,

Thanks.it worked.

If I use call back methods ,the scope of returning json will becoming the problems,because I need perform some other extra calls to server and needs to retrieve the data .

can you suggest me what would be the best way?

Regards,

Kiran

former_member182372
Active Contributor
0 Kudos

use async and in attachRequestCompleted fire an event or use jQuery.Deferred so you will get notified once the data is loaded

like here

you can even do

  1. this.IamWaitingForYou = $.Deferred();


and use it in controller's methods

Answers (2)

Answers (2)

santhu_gowdaz
Active Contributor
0 Kudos

var url = root + "/idmrest/v72alpha/entries/0";

oModelData.loadData(url);

var aData = oModel1.getProperty("/d/results");  //If it is having single data then,("/d"). Debug it you will come to know.

var zkey = aData.MSKEYVALUE;//for single data

for (var i = 0; i < aData.length; i++)

{

    console.log(aData[i].MSKEYVALUE);//for multiple data.

}

former_member182862
Active Contributor
0 Kudos

I believe that you need to wait for the response from the server. try this

oModelData.attachRequestCompleted(function() {

     this.getProperty("/ENTRIES/MSKEYVALUE/VALUE");

});

thanks

-D

former_member182862
Active Contributor
0 Kudos

can you put it in a global model? like this

oModelData.attachRequestCompleted(function() {

     var m = new sap.ui.model.json.JSONModel({

        zkey: this.getProperty("/ENTRIES/MSKEYVALUE/VALUE")

     });

     sap.ui.getCore().setModel(m, 'global');

});

In future, you can get it this way.

sap.ui.getCore().getModel('global').getProperty('/zkey');


-D