javascript 對象與字串相互轉換函式 JSON.stringify 和 JSON.parse 的使用__函數

來源:互聯網
上載者:User
JSON.stringify()

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified. Syntax

JSON.stringify(value[, replacer[, space]])
Parameters value The value to convert to a JSON string. replacer  Optional A function that alters the behavior of the stringification process, or an array of  String and  Numberobjects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string. space  Optional A  String or  Number object that's used to insert white space into the output JSON string for readability purposes. If this is a  Number, it indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used. If this is a  String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used. Return value

A JSON string representing the given value. Description

JSON.stringify() converts a value to JSON notation representing it: Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification. Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics. If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array). JSON.stringify can also just return undefined when passing in "pure" values like JSON.stringify(function(){})or JSON.stringify(undefined). All symbol-keyed properties will be completely ignored, even when using the replacer function. Non-enumerable properties will be ignored

JSON.stringify({});                  // '{}'JSON.stringify(true);                // 'true'JSON.stringify('foo');               // '"foo"'JSON.stringify([1, 'false', false]); // '[1,"false",false]'JSON.stringify({ x: 5 });            // '{"x":5}'JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) // '"2006-01-02T15:04:05.000Z"'JSON.stringify({ x: 5, y: 6 });// '{"x":5,"y":6}' or '{"y":6,"x":5}'JSON.stringify([new Number(1), new String('false'), new Boolean(false)]);// '[1,"false",false]'JSON.stringify({ x: [10, undefined, function(){}, Symbol('')] }); // '{"x":[10,null,null,null]}'  // Symbols:JSON.stringify({ x: undefined, y: Object, z: Symbol('') });// '{}'JSON.stringify({ [Symbol('foo')]: 'foo' }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.