简体   繁体   中英

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). I would like to build a prototype for this JSON object so that I have all my functions available. 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.

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 ?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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