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.

3 Answers 3

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. 
}; 
Sign up to request clarification or add additional context in comments.

4 Comments

Hi , @Basarat, can I define it in this way ?this.parse= function(jsonstring){...}; thanks.
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.
Hi , @Basarat, So the toJson() also should use the prototype? thanks.
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.
2

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

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

Comments

1

try this code..

var arr_from_json = JSON.parse( json_string );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.