2013-03-22 130 views
1

所有,我看到很多关于如何在SO中解析json到js对象(或将json转换为js对象)的示例。但是我没有看到一个将json绑定到已经定义好的js对象的例子。现在,当我尝试制作它时,我遇到了一些麻烦。请帮助我查看它。谢谢。JSON绑定到Javascript对象

我做了什么至今看起来象下面这样:

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. 

我希望我的方向迄今为止要解决的问题是正确的,如果这是不对的,我希望你给我一些好评。

回答

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. 
}; 
+0

嗨,@Basarat,我可以这样定义它吗?'this.parse = function(jsonstring){...};'谢谢。 – 2013-03-22 05:52:39

+0

这样您将拥有每个实例的功能(对内存不好)。更何况你不能创建一个继承链。 – basarat 2013-03-22 05:54:34

+0

好的,我明白了。谢谢。 – 2013-03-22 05:56:30

1

试试这个代码..

var arr_from_json = JSON.parse(json_string); 
2

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

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