简体   繁体   English

如何在JSON模型中获取单个JSON对象?

[英]How to get a single JSON object in JSON Model?

I defined a default model in manifest.json: 我在manifest.json中定义了一个默认模型:

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

Then get a sap.ui.model.json.JSONModel by: 然后通过以下方式获取sap.ui.model.json.JSONModel:

 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: 我想要的是一个VehicleId为0001的车辆,相反,它以字符串形式返回了整个VEHICLES数组:

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 参考: 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", ): JSON结构( model.oData和数据的结果来自"/.../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. 我想分享我的解决方法(可以将它们放在这里吗?),我承认它非常丑陋,因此我希望通过使用UI5 API使其更加优雅。

Master.controller.js Master.controller.js

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

Detail.controller.js 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 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. 您应该始终使用getProperty来访问模型数据。 In your example you access vehicleId like that: 在您的示例中,您可以这样访问vehicleId:

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. model.getData()将为您提供对象。 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] . 并且一旦获得对象,就可以使用键,即model.getData().VEHICLES ,它将为您提供现在为array的值,以访问第三个元素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. 注意:这是您对json的一些假设,但如果可以做console.log(model.getData())或放置JSON.stringify(model.getData()) ,则将json结构粘贴到问题中,并让我们知道哪个特定元素您想要访问它将会很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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