gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:53:38 24 4
gpt4 key购买 nike

所有,我看到很多例子都在谈论如何在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com