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,
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 | |
| 6 | |
| 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.