cancel
Showing results for 
Search instead for 
Did you mean: 

Data through JSON file

Former Member
0 Kudos
116

Hello All

Have a json file(emp.json) which contains employee data, want to instantiate model in controller then want to make use in UI (bind it to ui form fields).

emp.json:

{

     firstName: "John",

     lastName: "Doe",

     birthday: {day:01,month:05,year:1982},

     address:[{city:"Heidelberg"}],

     enabled: true

}

test.controller.js

var oModel = new sap.ui.model.json.JSONModel("model/std.json");

sap.ui.getCore().setModel(oModel);

test.view.js:

var oTxt = new sap.ui.commons.TextField("txtField", {

     value: "{/firstName}"

  });

But output data from file is not displaying in text field, Please correct me if i coded wrongly.

Thanks

Sri

Accepted Solutions (1)

Accepted Solutions (1)

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Your JSON is invalid. If you've used that JSON as shown in your message above, you will probably be able to see an error in the console.

Problems with the JSON:

- the property names should be in double quotes

- the values 01 and 05 aren't accepted as numbers

Fix these issues and it should work fine.

I'm also assuming that you have matched the filenames (emp.json != std.json) 🙂

dj

Former Member
0 Kudos

Hello DJ

Thank you for reply.

Have take below json file but still not getting output.

model/std.json :

{

  firstName: "John",

  lastName: "Doe"

   

}

test.controller.js

var oModel = new sap.ui.model.json.JSONModel("model/std.json");

sap.ui.getCore().setModel(oModel);

test.view.js:

var oTxt = new sap.ui.commons.TextField("txtField", {

     value: "{/firstName}"

  });

Please tell me what is going wrong still .

Sri

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Hello Sri

You still haven't fixed the first issue - the property names need to be in quotes, like this:

{

  "firstName": "John",

  "lastName": "Doe"

}

dj

Former Member
0 Kudos

Hello DJ

Thank you very much, its worked.

Sri.

Answers (0)