cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing Nested JSON Data in Select Box in UI5 XML View

0 Kudos

Hi,

I have JSON which is of the following format:

{  
   "Rowsets":{  
      "DateCreated":"2017-08-22T17:20:00",
      "Version":"15.0 SP4 Patch 8 (Jul 30, 2015)",
      "StartDate":"2017-08-22T16:20:00+0530",
      "EndDate":"2017-08-22T17:20:00+0530",
      "CachedTime":"",
      "Rowset":[  
         {  
            "Columns":{  
               "Column":[  
                  {  
                     "Name":"B_CATEGORY",
                     "SourceColumn":"B_CATEGORY",
                     "Description":"B_CATEGORY",
                     "SQLDataType":12,
                     "MinRange":1.0,
                     "MaxRange":1.0
                  }
               ]
            },
            "Row":[  
               {  
                  "B_CATEGORY":"Action"
               },
               {  
                  "B_CATEGORY":"Fiction"
               },
               {  
                  "B_CATEGORY":"Romance"
               },
               {  
                  "B_CATEGORY":"Thriller"
               }
            ]
         }
      ]
   }
}

And the following is the controller:

onInit: function(evt) {
var oModel =  new sap.ui.model.json.JSONModel("firstsapui5project/data.json");
this.getView().setModel(oModel);
  }

I want to access all B_CATEGORY values in the select box and I use the below code for it

<Select id="selStaff" items="{/Row}" change="handleStaffSelect">
            <core:ListItem key="{B_CATEGORY}" text="{B_CATEGORY}" />
</Select>

It doesn't work. I think there's some problem with this - items="{/Row}. Can anyone please help?

Accepted Solutions (1)

Accepted Solutions (1)

former_member185280
Active Contributor

"{/Rowsets/Rowset/0/Row}"

Answers (2)

Answers (2)

Sharathmg
Active Contributor

Loop through the local oModel in debugger console(while in action).

Here, you can loop through the possible methods to identify the exact path to the B_Category field.

SergioG_TX
Active Contributor
0 Kudos

the issue is with the path... you need to name it using the correct path, something like this may work for your items: "{Rowsets/RowSet/Row}" but you may still have an error bc of the nested arrays. Alternatively, please move the Row property at the first level:

{

Rowsets:{},

Rows: [] // something like this

}

and your current code should work

0 Kudos

I tried {Rowsets/RowSet/Row} earlier but like you said, it didn't work. Actually the system I am working on (MII) gives the output in JSON I have included in the question. It automatically includes those lines -

"Rowsets":{  
      "DateCreated":"2017-08-22T17:20:00",
      "Version":"15.0 SP4 Patch 8 (Jul 30, 2015)",
      "StartDate":"2017-08-22T16:20:00+0530",
      "EndDate":"2017-08-22T17:20:00+0530",
      "CachedTime":"",
      "Rowset":[  
         {  
            "Columns":{  
               "Column":[  
                  {  
                     "Name":"B_CATEGORY",
                     "SourceColumn":"B_CATEGORY",
                     "Description":"B_CATEGORY",
                     "SQLDataType":12,
                     "MinRange":1.0,
                     "MaxRange":1.0
                  }
               ]
            },

Can this data be accessed somehow or the initial lines be trimmed?

SergioG_TX
Active Contributor

iterate thru the Rowsets/Rowset array, if one of the items is Row, then you get the list.

var oTmp = yourJSONResponse; 
var rowSet = oTmp.Rowsets.Rowset; // assume you have Rowsets and a Rowset var row = rowSet[0] ? rowSet[0].Row || [] : []; // since you won't have multiple lists, right?

then bind row as your data model