Assign json stored in a file to a js variable - javascript

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

Related

XML parsing using Jquery where format is unknown

I dont know the XML format. but i want to parse it from a given XMLObject using Jquery. Is it possible if yes then how to do that.
You can do this using JSONP to convert your XML to a JSON object.
This blog posts covers it nicely and saves me rewriting the code.
http://weedygarden.net/2011/01/consuming-remote-xml-as-jsonp/

JSON file parsing issue due to javascript variables

I've been provided with a json file to show results in a tableview.
But when I tried json file validation using online json file editor then I am getting "Build tree failed" error.
I posted this message to my client about wrong json file and then he has posted some strange description which I am totally unaware.
Yes the json is not a pure json. It has javascript variables which have json variable. For now u can use a webview to evaluate the javascript variables and use the json value. For ex : dp_f : corresponds to list of departure train is a javascript array variable. U may have to eval the variable and store each array element is a new json with key as dp_f and value of array of these values
Please tell me how can I use this json file which is not pure. What is the way to parse such files ?
The only real way to evaluate JavaScript (which is really what you have, not JSON) is to evaluate JavaScript.
You will have to use some form of eval(). I recommend trying a JavaScript sandbox, if available to you. Of course the best option would be to get the data fixed.

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

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

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 :(

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