Convert an instance from a JS object to a json string format

  • 2020-03-30 04:13:53
  • OfStack

This article illustrates the JS object and json string format conversion method, to share for your reference. The specific implementation method 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();
        obj.Name = " Eight quit "
        obj.Age = 500;         //Object
is defined as a literal         var obj1 = { "Name": " Eight quit ", "Age": "500" };
        var arr = [{ "Name": " Eight quit ", "Age": "500" }, { "Name": " Eight quit 1", "Age": "1000" }];         //JSON format: the literal representation of JSON is stored as a string as a JSON format string
        var str = '{ "Name": " Eight quit ", "Age": "500" }';
        var jsonstrarr = '[{ "Name": " Eight quit ", "Age": "500" }, { "Name": " Eight quit 1", "Age": "1000" }];';
       
        //Converts the json string to a js object (array)
        var resobj = JSON.parse(str);
        alert(resobj.Name);     </script>
</head>
<body>
</body>
</html>

Interconversion of js objects and json objects in the front end:

I. JS object is converted into JSON

Handling: refers to a json2.js file and calls the json.stringify () method. Such as:

var data = new Object();
var json_data = JSON.stringify(data);

 
PS: json2.js this file can be downloaded by searching the Internet.

Second, JSON is converted into JS

1. Processing: turn json-formatted data into JS objects using $.parsejson (), a method of jQuery. Such as:

var json_data = $.getJSON();
var data = $.parseJSON(json_data);
(can be used to convert js arrays)

2. Method to convert json into js object:

var json = eval('(' + result + ')');

With the above expression, you've parsed the server-side response to the client's json-formatted string into a Json (formatted) object called "Json" that can be accessed by "json." or "Json []."
 
Transfer of js object and json object in the background:

The.net Framework 4 in the System. The Runtime. Serialization. The JavaScriptSerializer class is used to serialize and deserialize data

Example:

//Set the param antisequence column list 
List<ApplyPart> _ApplyPart = new JavaScriptSerializer().Deserialize<List<ApplyPart>>(param);

PS: about json operation, here again for you to recommend a few more practical json online tools for your reference:

Online JSON code verification, verification, beautification, formatting tools:
(link: http://tools.jb51.net/code/json)

JSON online formatting tool:
(link: http://tools.jb51.net/code/jsonformat)

Online XML/JSON interconversion tool:
(link: http://tools.jb51.net/code/xmljson)

Json code online formatting/beautification/compression/editing/conversion tools:
(link: http://tools.jb51.net/code/jsoncodeformat)

Online json compression/escape tool:

(link: http://tools.jb51.net/code/json_yasuo_trans)

C language style /HTML/CSS/json code format beautification tool:
(link: http://tools.jb51.net/code/ccode_html_css_json)


Related articles: