Javascript 如何将嵌套JSON映射到具有函数的对象结构?

Javascript 如何将嵌套JSON映射到具有函数的对象结构?,javascript,json,Javascript,Json,我想绘制这样一个结构图: { "stuff": { "onetype": [ {"id":1,"name":"John Doe"}, {"id":2,"name":"Don Joeh"} ], "othertype": {"id":2,"company":"ACME"} }, "otherstuff": { "thing": "some value"

我想绘制这样一个结构图:

{ 
    "stuff": {
        "onetype": [
            {"id":1,"name":"John Doe"},
            {"id":2,"name":"Don Joeh"}
        ],
        "othertype": {"id":2,"company":"ACME"}
    }, 
    "otherstuff": {
        "thing": "some value"
     }
}
到一个包含2个对象数组(stuff和其他stuff)的对象。我想为这个JSON对象构建一个原型,这样我就有了所有可用的函数。我希望能够这样做(树是父对象)
tree.display()
其中显示函数将在数组中显示一种类型,并在包含对象上调用函数display

有没有一个简单的方法可以做到这一点?有人能给我举个例子说明怎么做吗? 我可以使用像
$.extend
这样的函数吗

这不起作用:

$(document).ready(function(){
    JSONString = '\
        {"stuff": \
            {"onetype": \
                [{"id":1,"name":"John Doe"},\
                {"id":2,"name":"Don Joeh"}],\
            "othertype": \
                {"id":2,"company":"ACME"}\
            }, \
        "otherstuff": {"thing": "some value" }\
        }';

    function Tree () {
        this.stuff = new StuffObject();
        this.otherstuff;
        this.showValue = function () {
            $("body").append(this.otherstuff.thing);
        }
    }

    function StuffObject () {
        this.onetype = new Array();
        this.othertype = new OthertypeObject();
    }

    function OthertypeObject () {
        this.id = 0;
        this.company = "unspecified";
        this.showCompany = function(){
            console.log(this.company);
        }
    }

    var firstTree = $.extend(true, new Tree, JSON.parse(JSONString));
    console.log(firstTree);
    firstTree.showValue();
    firstTree.stuff.othertype.showCompany();
});

如果您只想显示某个字段,可以尝试此操作,它将显示公司名称:

<script>
    alert("see this");
    JSONString = '\
        {"stuff": \
            {"onetype": \
                [{"id":1,"name":"John Doe"},\
                {"id":2,"name":"Don Joeh"}],\
            "othertype": \
                {"id":2,"company":"ACME"}\
            }, \
        "otherstuff": {"thing": "some value" }\
        }';

    function Tree () {
        this.stuff = new StuffObject();
        this.otherstuff;
        this.showValue = function () {
            $("body").append(this.otherstuff.thing);
        }
    }

    function StuffObject () {
        this.onetype = new Array();
        this.othertype = new OthertypeObject();
    }

    function OthertypeObject () {
        this.id = 0;
        this.company = "unspecified";
        this.showCompany = function(){
            console.log(this.company);
        }
    }

    var firstTree = $.extend(true, new Tree, JSON.parse(JSONString));
   // var x=json_encode(firstTree);
    console.log(firstTree);
    var x = JSON.stringify(firstTree.stuff.othertype.company);
    alert(x);

</script>

警惕(“见此”);
JSONString=\
{“东西”:\
{“一种类型”:\
[{“id”:1,“name”:“John Doe”}\
{“id”:2,“name”:“Don Joeh”}\
“其他类型”:\
{“id”:2,“公司”:“ACME”}\
}, \
“otherstuff”:{“thing”:“some value”}\
}';
函数树(){
this.stuff=新的StuffObject();
这是其他的东西;
this.showValue=函数(){
$(“body”).append(this.otherstuff.thing);
}
}
函数StuffObject(){
this.onetype=新数组();
this.othertype=新的OthertypeObject();
}
函数OthertypeObject(){
此参数为0.id;
this.company=“未指定”;
this.showCompany=函数(){
console.log(this.company);
}
}
var firstTree=$.extend(true,new Tree,JSON.parse(JSONString));
//var x=json_encode(firstTree);
console.log(firstTree);
var x=JSON.stringify(firstTree.stuff.othertype.company);
警报(x);

$.extend(新树,JSON.parse(plain_JSON));Tree是一个包含名为stuff和otherstuff的字段的原型,原型中的函数无法正常工作。log(tree.otherstuff.thing)将显示值,使用此值的函数会给我一个undefined@MaartenArits向我们展示你的代码,还有“使用这个值的函数给我一个未定义的值”是我无法解析的。我不打算投你反对票,但请再试一次。@Daedalus,谢谢你的评论,我对这一点还不熟悉。我粘贴了一些代码,试图将对象映射到第一层workst,但它还不是深度副本