简体   繁体   中英

How to get a single JSON object in JSON Model?

I defined a default model in manifest.json:

"dataSources": {            
        "mainService": {
            "uri": "/.../vehicleCollection",
            "type": "JSON"
        }
    },

Then get a sap.ui.model.json.JSONModel by:

 var model = oView.getModel();    
 var oObject = model.getJSON("/VEHICLES(0001)");

What I want is a VEHICLES which vehicleId is 0001, instead, it returned the whole VEHICLES array in string:

oObject = "{"VEHICLES":[{"vehicleId":"0001","route":...} {"vehicleId":"0005","route":...} ..."

I was wondering is there any convenient way to get a single data object ? I am trying to bind this data to my detail page in master-detail template.

Ref: https://sapui5.netweaver.ondemand.com/#docs/api/symbols/sap.ui.model.json.JSONModel.html

====Update====

JSON structure (result of model.oData and data get from "/.../vehicleCollection", ):

{"VEHICLES":[ { "vehicleId":"0001", "route":"..", ... }, { "vehicleId":"0005", "route: "..", ... }] }

====update=====

I'd like to share my workaround(is it OK to put them here?), I admit it's very ugly, so I hope to make it more elegant by using UI5 API.

Master.controller.js

_showDetail : function (oItem) { var bReplace = !Device.system.phone; this.getRouter().navTo("object", { objectId : oItem.getBindingContext().getProperty("vehicleId") }, bReplace); },

Detail.controller.js

_onBindingChange : function () {
var oView = this.getView(),
    oElementBinding = oView.getElementBinding(),
    oViewModel = this.getModel("detailView"),
    sPath = oElementBinding.getPath(),
    oResourceBundle = this.getResourceBundle(),
    ObjectId = sPath.match(/'(.*?)'/)[1],

var JSONObject = oView.getModel().getJSON();

JSONObject = JSON.parse(JSONObject);

function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}

var oneVehicle = getObjects(JSONObject, "vehicleId", ObjectId);

oViewModel.setProperty("/route", oneVehicle[0]['route']);
},

Detail.view.xml

<ObjectStatus text="{detailView>/route}"/>

The proposed solution works but it does not embrace best practices and I strongly recommend not to start to code like that. You should always use getProperty to access model data. In your example you access vehicleId like that:

var vehicleId = model.getProperty("/VEHICLES/3/vehicleId");

If you need the whole vehicle use:

var vehicle = model.getProperty("/VEHICLES/3");

model.getData() will get you the object. And once you get the object you can use the key ie model.getData().VEHICLES which will give you the value that is array now to access the third element model.getData().VEHICLES[3] .

So your answer would be :

model.getData().VEHICLES[3].vehicleId ;

Note: This is some assumption of your json but if can do a console.log(model.getData()) or place JSON.stringify(model.getData()) paste your json structure to the question and let us know which particular element you want to access it would be easy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM