Skip to Content
0

UI5 Loading json file

Mar 08, 2017 at 12:51 PM

131

avatar image
Former Member

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?.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Jun Wu Mar 08, 2017 at 06:28 PM
0

oMydata.loadData("tabLayout/Mydata.json",false);

file loading is aysnc. that's why you cannot get the data immediately after the data load call.

you can make it as sync, you can supply second parameter value false; make sure have the right path to the file.

Share
10 |10000 characters needed characters left characters exceeded
Srikanth KV Mar 08, 2017 at 04:55 PM
0

Check your network tab in Chrome to see if there is any request sent for JSON file

I think the issue is related to path where JSON file is stored...can you show the screenshot of project structure..

Alternatively you can instantiate JSON models directly from manifest.json (suggested way)

Share
10 |10000 characters needed characters left characters exceeded
A K Mar 09, 2017 at 08:16 AM
0

It is working as expected, please have a look on the below url.

http://plnkr.co/edit/be7JBFq8aPduun04PdHO?p=preview

Regards,

Karthik A

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

That Plunker link uses setData and NOT loadData. I thought I should mention that, but thanks all the same.

0