Json.parse parsing JSON string, error in line break

Source: Internet
Author: User

The JSON string that was returned in the background today was wrong, and a textarea was found to have a newline in the submitted field.

Error reason: Json.parse to JSON string encountered some special characters need to be escaped first,

Can be escaped with str.replace (/\n/g, "\\\\n"), ' \ \ ' to a single ' \ ', ' \\n ' to ' \ n ', and finally to ' \\n ' so that it can be converted to an object with parse to ' \ n ', the extracted string is set to the HTML text \ N is parsed as a newline.

The ECMA script indicates the characters in the JSON string that need to be escaped:

Test:

1.var kks= "{" id ": 2," name ": null}" Reviver traversal of the first two is the key value in the JSON string, the null value temporarily does not know what is (Prototype object?). )。

2.json string has a value of undefined parsing will error

2.json.stringify ignores objects with a value of undefined

Syntax: The value of undefined is not included in the scope can be resolved, so will error, haha.

Reference: The English version of the ECMA specification: HTTP://WWW.ECMA-INTERNATIONAL.ORG/ECMA-262/5.1/

English: http://lzw.me/pages/ecmascript/#611

Json.parse parsing JSON string, error in line break

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.