cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Create a JSON Model from data in file

SandipAgarwalla
Active Contributor
0 Likes
2,827

Hi

How do we create a JSON Model for the data which is defined in a .json file in the project resources

e.g. my data is stored in data.json file which is under models folder.

Now I need to create a model for it, I tried


var json = new sap.ui.model.json.JSONModel("/models/data.json");

json.setData({modelData:"sampleData"});


json.attachRequestCompleted(oData, function(){

  sap.ui.getCore().setModel("json",json);

     var nLength = json.getData().d.results.length; 

  alert(nLength);

  });

But it didn't work. The data.json has the format


var sampleData = [

{<somedata>},

{<somedata>}

];

Any pointers??

Thanks

Sandip

View Entire Topic
Former Member
0 Likes

Hi Sandip,

I just had a look in the SDK examples. This is one way to load JSON data from a file:


// Data is fetched here

  jQuery.ajax("Data.json", {   // load the data from a relative URL (the Data.json file in the same directory)

  dataType: "json",

  success: function(data){

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

  view.setModel(oModel);

  }

  });

In that case the JSON file format is like this:


{

  "countries" : [ {

  "name" : "Greece",

  "short" : "GR",

  "detailInfo": {

  "capital": "Athens",

  "population": 10787690,

  "area": 131990,

  "currency": "Euro",

  "flagUrl": "http://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Flag_of_Greece.svg/200px-Flag_of_Greece.svg..."

  }

  },...

Hth,

Simon