简体   繁体   English

如何使用函数将嵌套的JSON映射到Object结构?

[英]How to map nested JSON to an Object structure with functions?

I would like to map a structure like this: 我想映射这样的结构:

{ 
    "stuff": {
        "onetype": [
            {"id":1,"name":"John Doe"},
            {"id":2,"name":"Don Joeh"}
        ],
        "othertype": {"id":2,"company":"ACME"}
    }, 
    "otherstuff": {
        "thing": "some value"
     }
}

to an object containing 2 arrays of objects (stuff and other stuff). 到包含2个对象数组(对象和其他内容)的对象。 I would like to build a prototype for this JSON object so that I have all my functions available. 我想为此JSON对象构建一个原型,以便我可以使用所有功能。 I would like to be able to do something like this (tree being the parent object). 我希望能够做这样的事情(树是父对象)。 tree.display() where the display function will go thought the array one type and call the functions display on the containing objects. tree.display() ,其中的显示函数将以一种类型的数组进入数组,并在包含的对象上调用函数display。

Is there an easy way of doing this? 有一个简单的方法吗? Can somebody point me to an example of how to do this? 有人可以给我指出如何做到这一点的例子吗? Can I use a functions like $.extend ? 我可以使用$.extend类的功能吗?

this is not working: 这不起作用:

$(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();
});

If you only Want to display some certain field you can Try this it will give the Company name: 如果您只想显示某些特定字段,则可以尝试此操作,它将给出公司名称:

<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>

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

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