根据kendo模型手动解析json数据 [英] Manually parse json data according to kendo model
问题描述
schema.model
,Kendo UI中的任何内置即用解决方案可解析JSON数据? 也许像
kendo.parseData(json,model)
这样会返回数组的对象?我正在寻找类似的东西,找不到任何内置的东西。然而,使用 Model.set
显然使用每个字段的解析逻辑,所以我最终写了这个功能非常好:
function parse(model,json){
//我用json数据初始化模型作为一个快速修复,因为
//设置id字段不似乎没有工作。
var parsed = new model(json);
var fields = Object.keys(model.fields); (var i = 0; i< fields.length; i ++){
parsed.set(fields [i],json [fields [i]]);
}
返回解析;
}
其中模型
定义(或简单地 datasource.schema.model
)和的
是原始对象。使用或修改它来接受和返回数组不应太难,但对于我的用例,我只需要一次要解析一个对象。 kendo.data.Model
json
Any built-in ready-to-use solution in Kendo UI to parse JSON data according to schema.model
?
Maybe something like kendo.parseData(json, model)
, which will return array of objects?
I was searching for something like that and couldn't find anything built-in. However, using Model.set
apparently uses each field's parse logic, so I ended up writing this function which works pretty good:
function parse(model, json) {
// I initialize the model with the json data as a quick fix since
// setting the id field doesn't seem to work.
var parsed = new model(json);
var fields = Object.keys(model.fields);
for (var i=0; i<fields.length; i++) {
parsed.set(fields[i], json[fields[i]]);
}
return parsed;
}
Where model
is the kendo.data.Model
definition (or simply datasource.schema.model
), and json
is the raw object. Using or modifying it to accept and return arrays shouldn't be too hard, but for my use case I only needed a single object to be parsed at a time.
这篇关于根据kendo模型手动解析json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!