[Front-end development] JSON.parse parsing json string, the case of a line break error

The error encountered json string objects turn back to return today, found a textarea field submitted in a line break.

Cause: The encounter some special characters JSON.parse turn json string need to escape, as shown in Figure

Can str.replace (/ \ n / g, "\\\\ n") escaped, '\\' turn into single '\', 'n-\\' turn into '\ n-', and finally turn into ' \\ n 'so that it can be used to transfer objects becomes parse' \ n ', is set to the character string extracted html text \ n is interpreted as line breaks.

test:

1.var kks = "{" id ": 2," name ": null}" is the two front traverse reviver json string key, they did not know what null is (prototype Object?).

2.json string has undefined value being given parse

2.JSON.stringify ignores the target value is undefined

Syntax states: undefined value is not included in the scope can be resolved, it will complain, ha ha.

 

 

Cause: The encounter some special characters JSON.parse turn json string need to escape, as shown in Figure

Can str.replace (/ \ n / g, "\\\\ n") escaped, '\\' turn into single '\', 'n-\\' turn into '\ n-', and finally turn into ' \\ n 'so that it can be used to transfer objects becomes parse' \ n ', is set to the character string extracted html text \ n is interpreted as line breaks.

test:

1.var kks = "{" id ": 2," name ": null}" is the two front traverse reviver json string key, they did not know what null is (prototype Object?).

2.json string has undefined value being given parse

2.JSON.stringify ignores the target value is undefined

Syntax states: undefined value is not included in the scope can be resolved, it will complain, ha ha.

 

 

Guess you like

Origin www.cnblogs.com/xiaohuizhang/p/12186660.html