A json format error occurs when the backend receives a string in json format

Description of the problem : During the interface test, a problem occurred. The json format was wrong. One of the parameters passed by the front end in the interface was a string. This string is data in json format. This parameter needs to be executed in the background. Business, what the business needs is data in json format, but when testing the interface, it is found that the json format is wrong, that is, the json data passed in cannot be parsed when the business is executed.

Find the problem : After finding the problem, the interface uses @RequestBody to receive the data sent by the front end. Those data are in json format, that is to say, the parameter is wrapped with a layer of double quotation marks required by json format. The json in the parameter is natural It becomes a single quotation mark, so the data cannot be parsed in the background.

Data sent by the front end (json format):

"params":"{'code':'123456'}"

Solve the problem : After I get the parameter sent by the front end, do the following (replace all single quotes with double quotes):

String p = params.replaceAll("'", String.valueOf((char) 34));

Then you can see that the operation is successful. . . . . .

Guess you like

Origin blog.csdn.net/Hubery_sky/article/details/132047349