版权声明:jathams(人勤事事易,人懒事事难) https://blog.csdn.net/Jatham/article/details/82792862
Serializing Objects
javascript中经常要跟json数据打交道,前端显示需要json,后台接受也可能需要json,远程接口也会使用json、、、等等。所以今天总结下js中使用json的情况;
- JSON对象
js中的普通object对象就是JSON对象。
eg:
var jsonArr = [];
var json1 = {};//empty jsonObject
json1.name = "jathams";
var json2 = {"id":"2"};
jsonArr.push(json1);
jsonArr.push(json2);
这几个都是JSON对象。
-
将JSON对象解析为字符串:JSON.stringify(jsonObj)
var jsonStr = JSON.stringify(json1); var jsonArrStr = JSON.stringify(jsonArr);
-
json字符串转JSON对象JSON.parseJSON(jsonStr)
1>jQuery插件支持的转换方式: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 2>浏览器支持的转换方式(Firefox,chrome,opera,safari,ie9,ie8)等浏览器: JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串 注:ie8(兼容模式),ie7和ie6没有JSON对象,推荐采用JSON官方的方式,引入json.js。 3>Javascript支持的转换方式: eval('(' + jsonstr + ')'); //可以将json字符串转换成json对象,注意需要在json字符外包裹一对小括号 注:ie8(兼容模式),ie7和ie6也可以使用eval()将字符串转为JSON对象,但不推荐这些方式,这种方式不安全 eval会执行json串中的表达式。 4>JSON官方的转换方式: http://www.json.org/提供了一个json.js,这样ie8(兼容模式),ie7和ie6就可以支持JSON对象以及其stringify()和parse()方法; 可以在https://github.com/douglascrockford/JSON-js上获取到这个js,一般现在用json2.js。
-
解析JSON对象或者字符串:eval(jsonObj|jsonStr)
var obj1 = eval(json1); var obj2 =eval(jsonStr); var obj3 = eval(jsonArr); for(var i = 0;j<obj1.length;i++){ console.log(obj1[i].id); }
附一段文章摘自:《javascript权威指南》,有兴趣的可以看看
Object serialization is the process of converting an object’s state to a string from which
it can later be restored. ECMAScript 5 provides native functions JSON.stringify() and
JSON.parse() to serialize and restore JavaScript objects. These functions use the JSON
data interchange format. JSON stands for “JavaScript Object Notation,” and its syntax
is very similar to that of JavaScript object and array literals:
o = {x:1, y:{z:[false,null,""]}}; // Define a test object
s = JSON.stringify(o); // s is '{"x":1,"y":{"z":[false,null,""]}}'
p = JSON.parse(s); // p is a deep copy of o
The native implementation of these functions in ECMAScript 5 was modeled very
closely after the public-domain ECMAScript 3 implementation available at http://json
.org/json2.js. For practical purposes, the implementations are the same, and you can
use these ECMAScript 5 functions in ECMAScript 3 with this json2.js module.
JSON syntax is a subset of JavaScript syntax, and it cannot represent all JavaScript
values. Objects, arrays, strings, finite numbers, true , false , and null are supported and
can be serialized and restored. NaN , Infinity , and -Infinity are serialized to null . Date
objects are serialized to ISO-formatted date strings (see the Date.toJSON() function),
but JSON.parse() leaves these in string form and does not restore the original Date
object. Function, RegExp, and Error objects and the undefined value cannot be serial-
ized or restored. JSON.stringify() serializes only the enumerable own properties of an
object. If a property value cannot be serialized, that property is simply omitted from
the stringified output. Both JSON.stringify() and JSON.parse() accept optional second
arguments that can be used to customize the serialization and/or restoration process
by specifying a list of properties to be serialized, for example, or by converting certain
values during the serialization or stringification process. Complete documentation for
these functions is in the reference section.