SoFunction
Updated on 2025-02-28

JS object and json string format conversion example

This article describes the implementation method of JS object and json string format conversion, and is shared with you for your reference. The specific implementation method is as follows:

Copy the codeThe code is as follows:
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript">
        var obj = new Object();
= "Bajie"
        = 500;

//Define objects through literal form
var obj1 = { "Name": "Bajie", "Age": "500" };
var arr = [{ "Name": "Bajie", "Age": "500" }, { "Name": "Bajie1", "Age": "1000" }];

//JSON format: Use the literal representation of json as a string, then it is a json format string
var str = '{ "Name": "Bajie", "Age": "500" }';
var jsonstrarr = '[{ "Name": "Bajie", "Age": "500" }, { "Name": "Bajie1", "Age": "1000" }];';
       
//Convert json string to js object (array)
        var resobj = (str);
        alert();

    </script>
</head>
<body>
</body>
</html>

Transfer between js and json objects in front-end:

1. Convert JS object to JSON

Processing: Refer to a file and call the() method. For example:

Copy the codeThe code is as follows:
var data = new Object();
var json_data = (data);

 
PS: This file can be downloaded by searching online.

2. Convert JSON to JS

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

Copy the codeThe 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 the codeThe code is as follows:
var json = eval('(' + result + ')');

Through the above expression, the Json format string responding to the client on the server is parsed into a Json (format) object with the name "json", and data access can be performed through "json." or "json[]".
 
Transferring js objects and json objects in the background:
 
.NET Framework 4 is in. Serialize and deserialize data using JavaScriptSerializer class

Example:

Copy the codeThe code is as follows:
//Collection of param desequence column list
List<ApplyPart> _ApplyPart = new JavaScriptSerializer().Deserialize<List<ApplyPart>>(param);

PS: Regarding json operation, here are some more practical json online tools for your reference:

Online JSON code verification, inspection, beautification and formatting tools:
http://tools./code/json

JSON online formatting tool:
http://tools./code/jsonformat

Online XML/JSON mutual conversion tool:
http://tools./code/xmljson

json code online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

Online json compression/escaping tools:

http://tools./code/json_yasuo_trans

C language style/HTML/CSS/json code formatting and beautification tools:
http://tools./code/ccode_html_css_json

I hope this article will be helpful to everyone's web programming.