Skip to Content
0

Accessing Nested JSON Data in Select Box in UI5 XML View

Aug 23, 2017 at 10:11 AM

151

avatar image
Former Member

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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
christian libich Aug 23, 2017 at 03:48 PM
1

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

Share
10 |10000 characters needed characters left characters exceeded
Sharath M G Aug 23, 2017 at 11:57 AM
1

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.

Share
10 |10000 characters needed characters left characters exceeded
Sergio Guerrero Aug 23, 2017 at 10:16 AM
0

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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?

0

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

1