How to parse an object to JSON using jQuery? - javascript

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

Related

How can i convert CSV file to JSON format in React?

Do i have to make a variable and paste the CSV data and then convert to JSON or please specify some methods.
Thank You!
I'm not familiar enough with react to know if there is a necessity to make it react specific, but if you are open to using a library, this one is superb:
https://github.com/okfn/csv.js/
This library allows you to
Fetch the CSV from a url
Parse that CSV into an array
Serialize it back into CSV format if needed
So for you, I would say, use this library to fetch the data, parse the data, and then just JSON.stringify the array.
If you don't need to do it programmatically, why not just take the CSV, and put array brackets around it, and set your variable as that?
For example, my CSV:
Apple,Banana,Strawberry,
Pickle,Cucumber,Lettuce,
Milke,Bread,Onions,
Then I turn it into an array:
[['Apple','Banana','Strawberry'],
['Pickle','Cucumber','Lettuce'],
['Milk','Bread','Onions']]
You can just use built in Excel functions like concatenate to turn it into this format if your CSV file is really big and this is just a one-off exercise

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/

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

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

Categories

Resources