Dojo:如何从JSON加载对象(包含其他对象)? [英] Dojo: How to load an object (containing other objects) from JSON?
问题描述
保存到JSON很容易。只需使用这个: JSON.stringify(this)
。
从JSON加载并不简单。
- 我们不能只使用
this = JSON.parse(someJson)
因为 - 使用类似
lang.mixin(这样,JSON.parse(someJson))
将会获得这些功能但是
照片类:
define([...],function(...){
return declare(null,{
name:...,// String
url:.. 。,// String
complexProperty ... ... //其他类
someFunction1:function(...){..},
someFunction2:function(。 ..){..},
someFunction2:function(...){..}
}
));
相册类别:
define([...],function(...){
return declare(null,{
照片:[],/ *照片类型的数组)* /
someOtherProperty:...,
someOtherProperty:...,
someFunction1:function(...){..},
someFunction2:函数(...){..},
someFunction2:function(...){..},
toJson:function(){
return JSON.stringify (this); //从dojo / json
}
loadFromJson:function(jsonIn){
//如何做?
},
/ *这不行,因为方法将被覆盖* /
loadFromJson1:function(jsonIn){
this = JSON.parse(someJson);
},
/ *这保证我的方法保持原样,但我的孩子的方法不是(即:照片数组)* /
loadFromJson2:fun ction(jsonIn){
lang.mixin(this,JSON.parse(someJson));
},
/ *这似乎是一个令人愉快的工作。任何更好的方法来做到这一点* /
loadFromJson3:function(jsonIn){
this.someOtherProperty = jsonIn.someOtherProperty;
this.someOtherProperty = jsonIn.someOtherProperty;
foreach(jsonIn.photos:photoJson){
var newPhoto = new Photo();
newPhoto.loadfromJson(photoJson);
this.photos.add(newPhoto);
}
...所有其他属性递归设置。模型中的所有东西都需要这种方法...
}
}
));
我想你最好不要返回一个JSON对象它只包含您需要序列化的数据,而不是整个类。那么你的loadFromJson方法会更容易实现,你不会通过网络发送不必要的数据。示例toJson():
toJson:function(){
return JSON.stringify({
photos :this.photos,
someImportantProp:this.someImportantProp,
anotherProp:this.anotherProp
});
}
I have an object model that I want to be able to save. I am going to export it to JSON and then read it back in as JSON.
Saving to JSON is easy. Just use this: JSON.stringify(this)
.
Loading from JSON isn't as simple.
- We can't just use
this = JSON.parse(someJson)
because the methods wont be attached. - Using something like
lang.mixin(this, JSON.parse(someJson))
will get the functions but objects that are
Photo Class:
define([...], function(...){
return declare(null, {
name: ..., // String
url:..., // String
complexProperty:..., // Some other class
someFunction1: function(...){..},
someFunction2: function(...){..},
someFunction2: function(...){..}
}
));
Photo Album Class:
define([...], function(...){
return declare(null, {
photos: [], /* Array of type Photo (see above) */
someOtherProperty: ...,
someOtherProperty: ...,
someFunction1: function(...){..},
someFunction2: function(...){..},
someFunction2: function(...){..},
toJson: function(){
return JSON.stringify(this); // From dojo/json
}
loadFromJson: function(jsonIn){
// How to do this?
},
/* This doesn't work because methods will be overridden */
loadFromJson1: function(jsonIn){
this = JSON.parse(someJson);
},
/* This insures that my methods are kept intact but my childrens methods arn't (ie: the array of photos) */
loadFromJson2: function(jsonIn){
lang.mixin(this, JSON.parse(someJson));
},
/* This seems like an aweful lot of work. Any better ways to do this? */
loadFromJson3: function(jsonIn){
this.someOtherProperty = jsonIn.someOtherProperty;
this.someOtherProperty = jsonIn.someOtherProperty;
foreach(jsonIn.photos: photoJson){
var newPhoto = new Photo();
newPhoto.loadfromJson(photoJson);
this.photos.add(newPhoto);
}
... All other properties set recursively. All things in model now need this method ...
}
}
));
I think you would be better off returning a JSON object that contains just the data you need to serialize, not the whole class. Then your loadFromJson method would be a little easier to implement, and you wont be sending unnecessary data over the network. Example toJson():
toJson: function() {
return JSON.stringify({
photos: this.photos,
someImportantProp: this.someImportantProp,
anotherProp: this.anotherProp
});
}
这篇关于Dojo:如何从JSON加载对象(包含其他对象)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!