Home  >  Article  >  Web Front-end  >  JS object and json string format conversion example_javascript skills

JS object and json string format conversion example_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:33:001218browse

The example in this article describes the implementation method of converting JS objects and json string formats, and shares it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:







Interconversion between front-end js objects and json objects:

1. Convert JS objects into JSON

Processing: Reference a json2.js file and call the JSON.stringify() method. For example:

Copy code The code is as follows:
var data = new Object();
var json_data = JSON.stringify(data);


PS: The json2.js file can be downloaded by searching online.

2. Convert JSON to JS

1. Processing: Use a jQuery method $.parseJSON() to convert JSON format data into JS objects. For example:

Copy code The code is as follows:
var json_data = $.getJSON();
var data = $.parseJSON(json_data);
(can be used to convert js array)

2. Method to convert json into js object:

Copy code The code is as follows:
var json = eval ('(' result ')');

Through the above expression, the Json format string that the server responds to the client is parsed into a Json (formatted) object named "json", through "json." or "json[]" way to access data.

Conversion of js objects and json objects in the background:

.NET Framework 4 in System.Runtime.Serialization. Use JavaScriptSerializer class to serialize and deserialize data

Example:

Copy code The code is as follows:
//Deserialize the param list collection
List _ApplyPart = new JavaScriptSerializer().Deserialize>(param);

I hope this article can be helpful to everyone’s web programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn