JavaScript(js)中關於json格式的轉換
阿新 • • 發佈:2018-11-11
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.