Skip to Content
0
Mar 08, 2017 at 12:51 PM

UI5 Loading json file

8030 Views Last edit Mar 08, 2017 at 12:52 PM 2 rev

I am attempting to load a structured json file (entity model) and then bind it to a view. Also have other views use part of that json object (entity set) as well, but that's getting ahead of myself as I can't even get a simply json structure loaded yet. I'm using Eclipse as an editor, if that matters, with my json file being in the webContent directory, and my controller being in webContent/controller directory. We have tabLayout set in the index.html to point to the webContent directory.

I simply can not get my json file to load, here's a section of my controller code:

 .....
var TableController = Controller.extend("tabLayout.controller.testcontroller", {

onInit: function () { 
var oMydata = new sap.ui.model.json.JSONModel(); 
oMydata.loadData("tabLayout/Mydata.json"); 
console.log(JSON.stringify(oMydata.getData()));
....

When viewing in debug via chrome the odata section for oMydata shows no data.

I have also tried:

oReturns.loadData("Mydata.json");
oReturns.loadData("/Mydata.json");
oReturns.loadData("./tabLayout/Mydata.json");

but with no joy.

If I manually declare it in the console it's working, using setData.

My json file is in the format:

{ 
  "oMydata": { 
		[   
			{ "field1": "xyz",   
		  	  "field2": "abc"  
			}  
		]  
	     }
}

If anyone can see the obvious then do please let me know.

I get no error, that I can see when using loadData. Is there a good way of trapping whether an error has occurred and displaying it?.