1

All,I saw lot of examples talking about how to parse json to js object(or convert json to js object) in SO. But I didn't saw an example which is binding json to already defined js object. Now I have some trouble with it when I am trying to make it.Please help me to review it . thanks.

What I had done so far looks like below:

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 is the root which contains uncertain numbers of block which is self-included object .

and the Json is generate by Jackson, which looks like below .

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

}

the kind of code I imagine would looks like below :

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

I hope my direction so far to solve the problem is right, if it is not right, I hope you give me some kind comments.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Joe.wang
  • 11,537
  • 25
  • 103
  • 180

3 Answers3

2

You should define functions on the prototype:

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

This way they are shared between instances. You can access members of the current instance via this.variable syntax.

For more information on how prototype works you can check out : https://stackoverflow.com/a/4778408/390330

Your complete function will look something like:

top.prototype.parse= function(jsonstring){
    var data = JSON.parse( json_string );
    this.encoding = data.encoding; 
    // etc. 
}; 
Community
  • 1
  • 1
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Hi , @Basarat, can I define it in this way ?`this.parse= function(jsonstring){...};` thanks. – Joe.wang Mar 22 '13 at 05:52
  • That way you will have the function per instance (bad for memory). Not to mention you will not be able to create a inheritance chain. – basarat Mar 22 '13 at 05:54
  • Hi , @Basarat, So the `toJson()` also should use the `prototype`? thanks. – Joe.wang Mar 22 '13 at 06:00
  • Hi , @Basarat,Seems there is no easy way to bind the json to jsobject except writing `parse` by myself. I mean Is there any js libary to make it easier? thanks. – Joe.wang Mar 22 '13 at 06:04
2

try this one ..this one way to convert string to object..

 var response = eval('(' + data + ')');
tamilmani
  • 591
  • 6
  • 13
1

try this code..

var arr_from_json = JSON.parse( json_string );
Dineshkumar
  • 351
  • 1
  • 8