JSON绑定到Javascript对象

时间:2013-03-22 05:47:14

标签: javascript jquery json

所有,我看到很多例子讨论如何在SO中解析json到js对象(或将json转换为js对象)。但我没有看到一个将json绑定到已定义的js对象的示例。现在我在尝试制作它时遇到了一些麻烦。请帮我复习一下。感谢。

到目前为止我所做的事情如下所示:

top=function()
{
   this.encoding ='';
   this.nodes=[];
   this.lastid='';
   //I don't how to defined the attributes key in json which is a object.
   //I think there should exist a parse and toJson function; 
   //this.parse= function(jsonstring){...}; 
   //this.toJson=function(){var jsonstr=....;return jsonstr;};
};

group=functon()
{
   this.id='';
   this.type='';
   this.subnodes=[];
   this.tagname='';
   //....
}

top是包含不确定数量block的根,它是自包含的对象。

并且Json由杰克逊生成,如下所示。

{
"nodes": [
    {
        "type": "group",
        "id": 11,
        "tagName": "blockrow",
        "prefix": "aa",
        "cutomTag": null,
        "attributes": {
            "width": "12"
            //...more
        },
        "subNodes": [
            {
                "type": "group",
                "id": 111,
                "tagName": "blockcol",
                "prefix": "aa",
                "cutomTag": null,
                "attributes": {
                    "width": "4"
                },
                "subNodes": [
                    {
                        "type": "group",
                        "id": 1111,
                        "tagName": "section",
                        "prefix": "aa",
                        "cutomTag": null,
                        "attributes": {
                            "title": "NewSection",
                            "width": "12"
                        },
                        "subNodes": [
                            {
                                "type": "leaf",
                                "id": 11111,
                                "tagName": "message",
                                "prefix": "aa",
                                "cutomTag": null,
                                "attributes": {
                                    "key": "aa_login_success"
                                }
                            }
                        ]
                    }
                ]
            },
            {
                "type": "group",
                "id": 112,
                "tagName": "blockcol",
                "prefix": "aa",
                "cutomTag": null,
                "attributes": {
                    "width": "4"
                },
                "subNodes": [
                    {
                        "type": "group",
                        "id": 1121,
                        "tagName": "section",
                        "prefix": "aa",
                        "cutomTag": null,
                        "attributes": {
                            "title": "NewSection",
                            "width": "12"
                        },
                        "subNodes": [
                            {
                                "type": "leaf",
                                "id": 11211,
                                "tagName": "message",
                                "prefix": "aa",
                                "cutomTag": {
                                    "type": "cutomTag",
                                    "beginPos": 20,
                                    "endPos": 50,
                                    "id": -1
                                },
                                "attributes": {
                                    "key": "aa_login_failed"
                                }
                            }
                        ]
                    }
                ]
            },
            {
                "type": "group",
                "id": 113,
                "tagName": "blockcol",
                "prefix": "aa",
                "cutomTag": null,
                "attributes": {
                    "width": "4"
                },
                "subNodes": null
            }
        ]
    },
    {
        "type": "group",
        "id": 12,
        "tagName": "blockrow",
        "prefix": "aa",
        "cutomTag": null,
        "attributes": {
            "width": "12"
        },
        "subNodes": [
            {
                "type": "group",
                "id": 121,
                "tagName": "blockcol",
                "prefix": "aa",
                "cutomTag": null,
                "attributes": {
                    "width": "6"
                },
                "subNodes": null
            },
            {
                "type": "group",
                "id": 122,
                "tagName": "blockcol",
                "prefix": "aa",
                "cutomTag": null,
                "attributes": {
                    "width": "6"
                },
                "subNodes": null
            }
        ]
    }
],
"version": 1,
"encoding": "unicode",
"lastId": 1

}

我想象的代码如下所示:

var curTop= new top(); 
curTop.parse(jsonstring);
//manipulate the curTop object...
//...
var jsonStr=curTop.toJson();
//convert object to json.

我希望我到目前为止解决问题的方向是对的,如果不对,我希望你能给我一些好评。

3 个答案:

答案 0 :(得分:2)

您应该在原型上定义函数:

top.prototype.parse= function(jsonstring){...}; 

这样它们在实例之间共享。您可以通过this.variable语法访问当前实例的成员。

有关原型如何运作的更多信息,您可以查看:https://stackoverflow.com/a/4778408/390330

您的完整功能将如下所示:

top.prototype.parse= function(jsonstring){
    var data = JSON.parse( json_string );
    this.encoding = data.encoding; 
    // etc. 
}; 

答案 1 :(得分:2)

尝试这个..这种将字符串转换为对象的方法..

 var response = eval('(' + data + ')');

答案 2 :(得分:1)

试试这段代码..

var arr_from_json = JSON.parse( json_string );