Search code examples
javascriptjsonodatasapui5

Accessing JSON object in SAPUI5 via xsodata


I try to access the value of my json objects in sap ui5, but the getproperty function cannot access the required data. But, at first, I have created a xsodata file with some service definitions, e.g.

"CUSTOMER_ATTR_G3" as "Customers";

Then I try to get these data in view.js file with the following code:

oModel.loadData("UserInterface_G3/SERVICES/CUSTOMER_ATTR_G3.xsodata/Customers?$select=CUSTOMER_ID,CUSTOMER_DESCRIPTION&$format=json");

When I am using console.log(oModel) I see in the odata section that the values are in the object but I cannot access to them. The following screenshot should show the structure of the object: Object structure

I tried for instance:

console.log(oModel.getProperty('/CUSTOMER_DESCRIPTION'));

or

console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));

But I cannot access the values of the object.

Does anybody have an idea on that?


Solution

  • console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));

    You need to access your Property like that:

    console.log(oModel.getProperty('d/results/0/CUSTOMER_DESCRIPTION'));  
    

    You forgot the position inside of your Array. Your Path needs the position, so if you want to get the first entry CS_0001 then you have to write result/0/CUSTOMER_DESCRIPTION.

    EDIT:

    Actually it depends on your Model, how you have to access the Property. Can you pls show me how you defined your oModel?