Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<script type="text/javascript">
var myJSONObject = {"bindings": [
    {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
    {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
    {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};

function send(myJSONObject){
    var c = document.createElement('div');
    var t = document.createTextNode(myJSONObject.bindings[1].regex);
    c.appendChild(t);
    document.body.appendChild(c);
}
</script>
</head>
<body>
    <input type="button" value="go" onclick="send();" />
</body>

Can i access a json object in the above way by directly passing json object into a js function and including the value of it as the value of the text node - if not what are the things i am missing out? I want the dynamically created div to have the value of the json object in its text node. Thanks!

share|improve this question
Please accept answers that solve your problem by clicking on the green question mark as well. – missingno Jul 3 '11 at 21:30

3 Answers

You need to either use the globally defined object or pass the object along with the function:

Either:

function send(){
    var c = document.createElement('div');
    var t = document.createTextNode(myJSONObject.bindings[1].regex);
    c.appendChild(t);
    document.body.appendChild(c);
}

with

<input type="button" value="go" onclick="send();" />

OR

function send(myJSONObject){
    var c = document.createElement('div');
    var t = document.createTextNode(myJSONObject.bindings[1].regex);
    c.appendChild(t);
    document.body.appendChild(c);
}

with

<input type="button" value="go" onclick="send(myJSONObject);" />
share|improve this answer
Thank u so much..! – practical Jul 3 '11 at 20:25

In your code, you are declaring a global myJSONObject and then declaring a function argument also called myJSONObject.

The global one would be accessible from within the function except that you are masking it with the one in the argument list. You then call send without passing in any object. The result being that the myJSONObject accessible inside the function, i.e. the one from the argument list, is null.

You could either remove the argument from send and just use the global myJSONObject, or you have to ensure you pass it when you call send, i.e. send(myJSONObject);.

share|improve this answer

I have put together a little JSON sample that iterates over a JavaScript object and posts the property values to a cross domain server that is hosts by a DotNet.aspx page that then converts a C# object to a JSON string that is then posted back to the browser and converted back to a JavaScript object without having to use Window.Eval()

The resultant JavaScript object is then finally past back to a call-back function that is ready to uses and the code does not need 3rd party libraries, works in net framework 2.0 and upwards and has been tested with IE6-IE9, Firefox plus it's lightweight.

The code also show an example of using document.createElement('div');

Click my name or see http://www.flashinvader.com/developers_corner/simple_json_objects_using_javascript_and_aspx.html for full details

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.