Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing an object to JSON

People also ask

How do I save an object to JSON?

Python – Convert Class Object to JSON To convert a Python Class Object to JSON String, or save the parameters of the class object to a JSON String, use json. dumps() method.

How do you serialize a JSON object?

NET objects as JSON (serialize) To write JSON to a string or to a file, call the JsonSerializer. Serialize method. The JSON output is minified (whitespace, indentation, and new-line characters are removed) by default.

How do you serialize an object?

To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.

Can we serialize object?

Call JObject's ToString(Formatting. None) method. Alternatively if you pass the object to the JsonConvert. SerializeObject method it will return the JSON without formatting.


You're looking for JSON.stringify().


Download https://github.com/douglascrockford/JSON-js/blob/master/json2.js, include it and do

var json_data = JSON.stringify(obj);

Just to keep it backward compatible I load Crockfords JSON-library from cloudflare CDN if no native JSON support is given (for simplicity using jQuery):

function winHasJSON(){
  json_data = JSON.stringify(obj);
  // ... (do stuff with json_data)
}
if(typeof JSON === 'object' && typeof JSON.stringify === 'function'){
  winHasJSON();
} else {
  $.getScript('//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.min.js', winHasJSON)
}