May 08, 2021 JSON
1. The JS object is converted to JSON
3. The conversion method supported by the jQuery plug-in
The code is as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
var obj = new Object();
obj.Name = "八戒"
obj.Age = 500;
//通过字面量的形式来定义对象
var obj1 = { "Name": "八戒", "Age": "500" };
var arr = [{ "Name": "八戒", "Age": "500" }, { "Name": "八戒1", "Age": "1000" }];
//JSON格式:将json的字面量表示形式,当做字符串存放,则为json格式字符串
var str = '{ "Name": "八戒", "Age": "500" }';
var jsonstrarr = '[{ "Name": "八戒", "Age": "500" }, { "Name": "八戒1", "Age": "1000" }];';
//将json字符串转换成js对象(数组)
var resobj = JSON.parse(str);
alert(resobj.Name);
</script>
</head>
<body>
</body>
</html>
Interring between the front-end js object and the json object:
Processing: Refers to a json2 .js file and calls the JSON.stringify() method. For example:
var data = new Object();
var json_data = JSON.stringify(data);
PS: json2.js this file can be downloaded by searching the Internet.
1. Processing: Use one of jQuery's methods,$.parseJSON() to convert JSON-formatted data into JS objects. For example:
var json_data = $.getJSON();
var data = $.parseJSON(json_data);
(Can be used to convert an array of js)
2. Ways to convert Jason to a js object:
var json = eval('(' + result + ')');
With this expression above, the string in the Johnson format of the server-side response to the client is parsed into a Johnson object with the name "json" and "json."
Or the "json" method can be used for data access.
Example:
//将param反序列列list集合
List<ApplyPart> _ApplyPart = new JavaScriptSerializer().Deserialize<List<ApplyPart>>(param);
$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象
反过来,使用 serialize 系列方法:如:var fields = $("select, :radio").serializeArray();
Browser-supported conversion methods (Firefox, chrome, opera, safari, ie9, ie8) and other browsers
The code is as follows:
JSON.parse(jsonstr); //可以将json字符串转换成json对象
JSON.stringify(jsonobj); //可以将json对象转换成json对符串
Note: ie8 (compatible mode), ie7 and ie6 do not have JSON objects and require the introduction of json .js or json2 .js.