on ‎2013 Dec 03 5:52 PM
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
Request clarification before answering.
Hi Sandip,
JSON format could be validated with JSONLint - The JSON Validator.
Calling method setData is not required.
Below example might help
http://plnkr.co/edit/JMfSKGrA1xUimj9kzamO?p=preview
Thanks and Regards, Venkatesh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Simon, Venkat
I tried both the approaches, but still get a problem - 4040 Not found
GET http://localhost:8080/<projectName>/models/data.json 404 (Not Found)
So the problem is somewhere else. I tried loading the resources but no help
sap.ui.localResources("<projectName>/models");
BTW, the json defined in the file had a minor issue. that is fxed - thanks venkat
Regards
Sandip
HI Venkat
Still cant get it to work. I have the view defined in a separate folder.
so i tried
var json1 = new sap.ui.model.json.JSONModel("./models/data.json");
<note the period before /model> but error
GET http://localhost:8080/<projectName>/models/data.json 404 (Not Found)
Thanks
Sandip
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 9 | |
| 7 | |
| 7 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.