javascript - JSON 绑定(bind)到 Javascript 对象

标签 javascript jquery json

所有,我看到很多例子都在谈论如何在SO中将json解析为js对象(或将json转换为js对象)。但是我没有看到将 json 绑定(bind)到已经定义的 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 由 Jackson 生成,如下所示。

{
"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.

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

最佳答案

你应该在原型(prototype)上定义函数:

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

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

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

你的完整函数看起来像这样:

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

关于javascript - JSON 绑定(bind)到 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15563565/

相关文章:

jquery - 更改自定义模板中的 href

javascript - 如何检查json中数组的长度

javascript - 找到位于二次曲线上的两点之间的控制点

javascript - 无法将带有jquery的类添加到datepicker

javascript - .innerHTML 方法不返回字符串值时出现问题

javascript - 如何使用 mixin 覆盖这个函数?

javascript - 如何使用脚本禁用当前行的输入文本值

php - 我在使用 jquery 将图像上传到数据库时在 php 中遇到错误,变量中的数据可能没有传递

javascript - 如何在 JS 中选择具有特定值的数组中的对象

php - 如何将 jSON 转换为 XML