Skip to Content

Accessing Nested JSON Data in Select Box in UI5 XML View

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?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Aug 23, 2017 at 03:48 PM

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

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 23, 2017 at 11:57 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 23, 2017 at 10:16 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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