Godlike Godlike - 2 months ago 7
JSON Question

SAP/Open UI 5 load object data from JSON

I have a problem with Open UI5 / SAP. I have a JSON file
and I am showing a list. If you click on the list item you get some information about the entry.

So my Code looks like this:

JSON:

[{
"ID": 124,
"members" : [{
"member1" : "Person1", "Name" : "Test"
}]
}]


Controller:

...
_onRouteMatched : function (oEvent) {
var oArgs, oView;
oArgs = oEvent.getParameter("arguments");
oView = this.getView();

oView.bindElement({
path : "/Employees('" + oArgs.ID+ "')",
events : {
change: this._onBindingChange.bind(this),
dataRequested: function (oEvent) {
oView.setBusy(true);
},
dataReceived: function (oEvent) {
oView.setBusy(false);
}
}
});
},
...


View:

...
<f:content>
<Label text="{i18n>ID}" />
<Text text="{ID}" />
<Label text="{i18n>members}" />
<Text text="{members}" />
...


My problem is that it shows under members: "[object Object]". I tried everything to access the object but I coulnd't.

Does anyone have an idea how to access e.g. the name of member1 ?

By the way: I don't know if members contains anything. Sometimes it has member1, sometimes member1 and member2 and sometimes no member.

Answer

I found the answer: My JSON model is wrong!

It has to look like this:

[{
   "ID": 124, 
    "members" : [{
           "FirstName" : "Test",
           "LastName" : "Max"
      }]
}]

How to Access it:

View

...
<f:content>
                            <Label text="{i18n>ID}" />
                            <Text text="{ID}" />    
                            <Label text="{i18n>members}" />
                            <Text text="{members/0/FirstName}" />
...
Comments