如何使用JSON创建从对象类型继承的对象? [英] How to use JSON to create object that Inherits from Object Type?

查看:162
本文介绍了如何使用JSON创建从对象类型继承的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用JSON创建对象,但似乎没有使用JSON来创建具有特定对象类型的对象。

I know how to use JSON to create objects, but there doesn't seem to be away to use JSON to create an object that is of a specific object type.

这是一个Object的例子并创建它的一个实例:

Here's an example of an Object and creating an instance of it:

Person = function() { };
Person.prototype = {
    FirstName: null,
    GetFirstName: function() {
        return this.FirstName;
    }
};

//Create an instance of the Person Object
var me = new Person();
me.FirstName = "Chris";
alert(me.GetFirstName()); //alert the FirstName property

现在,我想使用JSON创建一个新的Person对象, GetFirstName函数对它起作用。

Now, I would like to use JSON to create a new Person object so that the GetFirstName function works on it.

这是我想做的事情(但这段代码不起作用):

Here's something like that I'm looking to do (but this code doesn't work):

var you = new Person() { FirstName: "Mike" };
// OR
var you = new Person{ FirstName: "Mike" };

无论如何使用JSON创建特定类型的对象?

Is there anyway to use JSON to create an object that is of a specific type?

更新:我的带有Person对象的示例只是为了简化问题。实际上,我无法修改我需要创建实例的实际对象的构造函数。这些对象是第三方库的一部分。

UPDATE: My sample with the Person object is just to simplify the question. In fact, I am unable to modify the constructors of the actual objects that I need to create instances of. The objects are part of a third-party library.

更新:使用下面的一些建议,我能够找到一种方法来创建一个继承自的对象原始的,并在它的构造函数中接受JSON。这很整洁!

UPDATE: Using some of the suggestions below, I was able to figure out a way to create an object that inherits from the original, and accept JSON in it's constructor. This is neat!

personWrapper = function(obj){
    for(var o in obj){
        this[o] = obj[o];
    }
};
personWrapper.prototype = new Person();

var you = new personWrapper({FirstName: "Chris"});
alert(you.GetFirstName());
alert(you instanceof Person); // returns True - we are successfully inheriting from Person!


推荐答案

我不这么认为。如果我是你,我会在Person类上创建一个函数来从JSON对象初始化。

I don't imagine so. I'd create a function on the Person class to initialise from a JSON object if I were you.

function Person() {
    this.loadFromJSON = function(json) {
        this.FirstName = json.FirstName;
    };
}

如果你不知道JSON对象事先代表什么类,也许在JSON中添加一个额外的变量。

If you didn't know what class the JSON object was representing beforehand, perhaps add an extra variable into your JSON.

{ _className : "Person", FirstName : "Mike" }

然后有一个'builder'函数解释它。

And then have a 'builder' function which interprets it.

function buildFromJSON(json) {
    var myObj = new json["_className"]();
    myObj.loadFromJSON(json);
    return myObj;
}






更新:既然你说该类是您无法更改的第三方库的一部分,您可以使用原型扩展该类,或者编写一个仅在外部填充该类的函数。


Update: since you say the class is part of a third-party library which you can't change, you could either extend the class with prototyping, or write a function which just populates the class externally.

例如:

Person.prototype.loadFromJSON = function(json) {
    // as above...
};

function populateObject(obj, json) {
    for (var i in json) {
        // you might want to put in a check here to test
        // that obj actually has an attribute named i
        obj[i] = json[i];
    }
}

这篇关于如何使用JSON创建从对象类型继承的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆