json: how to get the json-string into a javascript object - javascript

i am converting my ajax code from xml to json, but i am missing something basic here:
when i receive the json-string on the client-side, what is the recommended way to convert it into a javascript object.
for example i get this string:
{"connectionid":12345}
and i would like to do something like this:
alert(xmlhttp.responseText.connectionid);
thanks!

Use JSON.parse(), or eval(), if you like to live dangerously (or fully trust where your JSON comes from).
If you happen to be using jQuery, you get $.parseJSON().

Most browsers (the recent ones at least.. not IE7) have a native JSON object that you can use to parse and stringify JSON.
alert(JSON.parse(xmlhttp.responseText).connectionid);
In browsers that don't support the JSON object, you can either use a JSON parser from JSON.org or use eval(), however eval() is quite dangerous and i definitly don't advise you to use it.

Call eval on the response text.
var response = eval(xmlHttp.responseText);
alert(response.connectionId);

you could use eval
check this out : http://www.json.org/js.html
edit - oops, others typed faster :(

Related

Javascript object serialize readable

How do I serialize a javascript object.
I've tried JSON.stringify, which is great, but it serializes into JSON, with quotes around all the keys.
I'm looking for something that's not going to give me quotes around keys and still produce something readable, with padding.
Thanks.
I think you should look into YAML. We here use YAML, but let me look into it more, to see whether I can cite you some example.
Go through this stackoverflow discussion: Pure Javascript YAML library that supports both dump and load?

Assign json stored in a file to a js variable

I am working on a project and I was not able to figure out how to do this. I have a json file and need to assign it to a js variable to use the protovis visualization tools.
I tried searching on google but could not find any. Please let me know if someone knew how to do this. Thanks!
Use jQuery's parseJSON().
Description: Takes a well-formed JSON string and returns the resulting
JavaScript object. version added: 1.4.1jQuery.parseJSON( json )
jsonThe JSON string to parse. Passing in a malformed JSON string may
result in an exception being thrown.
If you're getting the file from your server via the XMLHttpRequest object, you can parse the json using the JSON.parse function:
var myJson = JSON.parse(response);
For browsers that don't support the JSON object you can get a library for it here: http://www.json.org/js.html

JSON vs Array and is there a way to convert one to the other then back again

I know in concept, or reality even they are essentially one in the same. However I know working with JSON objects (as of the time of writing this at least). That Altering the data within a given set of objects isn't the easiest thing. Of course I could be wrong, there could be an easy means of doing it all JSON. Which leads me to my actual question.
I am working almost entirely with JSON formatted Objects. Most of which I need to update frequently for storage for later use. Personally I would like to avoid sending data to and from the server to rebuild the JSON set of Objects every time. So I guess in all I am wondering if there is an equivalent to something like PHP's json_encode, json_decode? Either natively in JavaScript or through the jQuery Lib. and If not, how can I achieve something like that cause I feel (again at the moment) working with array's vs a JSON object is much easier (correct me if I am wrong).
The reason why this question pops up in your mind is because you don't understand neither Javascript or JSON enough.
The Javascript array is always the numerical array. There is no associate array in JavaScript like the one you get in PHP using json_decode(json, true);. When people talking about associate array in JavaScript, they are in fact using JavaScript objects and it's properties, i.e.,
var my_so_called_array = {};
my_so_called_array['first_item'] = 'hello world';
alert(my_so_called_array['first_item']); // 'hello world'
alert(my_so_called_array.first_item); // also 'hello world'
One of the best about JSON is that the syntax is a subset of the object literal notation. That means JSON string itself is valid code for representing the "associate array" in Javascript. The "conversion" couldn't be simpler, just eval() the string as code will do:
var my_data = eval(json_string);
Or, use jQuery.parseJSON() or JSON.parse():
var my_data = jQuery.parseJSON(json_string);
var my_data = JSON.parse(json_string);
The reason people prefer using jQuery.parseJSON() or JSON.parse() over eval() is because eval is evil:
It's slower
It doesn't check the validity of the JSON code, makes it suspect-able to injection attack.
Does the above explanation answer your question? You can find more information about JSON at www.json.org.
All modern browsers support JSON.stringify() and JSON.parse().
If you have to support older browsers, you can use jQuery's $.parseJSON().

XML construction using Javascript

Our app has a weird requirement which is to construct an xml using javascript and send that back to server as string.
I tried with jquery like this
$xmlT = $("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
$root = $("<notebook></notebook>"); //.attr("title", roottitle).
$root.attr("title", title);
$root.attr("id", id);
$root.appendTo($xmlT);
but am not able to get the xml as string back from the above variable.
is there some way or library using which i can construct xml and access that as string?
Regards,
Jeez
You can do things like createElement() etc on the XMLDoc returned from XMLHttpRequest.responseXML
http://www.w3schools.com/dom/dom_nodes_create.asp
So perhaps you can use XMLHttpRequest without actually doing a request, so you can get an object to manipulate.
You could then serialise the XML Dom using the XMLSerializer (for firefox) or xmlNode.xml for IE (metioned here How do I serialize a DOM to XML text, using JavaScript, in a cross browser way?)
You could take a look at this plugin
I found these jquery plugins xml2json & json2xml which is close to what I was looking for, if not the same.
For more info check stackoverflow entryXML <-> JSON conversion in Javascript

How to parse an object to JSON using jQuery?

I know to use eval() to parse JSON to an object, but how do I parse an object to JSON format using JavaScript?
Thanks.
The newer browsers support JSON.stringify. You can also download and include it yourself.
var json = JSON.stringify(yourObject);
Afaik jQuery does not provide such a method.
Usually if you have a json and want to access something underneath it you just put a dot, like if the name of your json is msg and you want the location parameter it would be:
msg.location

Categories

Resources