In JSON, double quotes in the cycle of use must be careful _javascript skills

Source: Internet
Author: User
Tags mongodb
1. If there are attributes in the JSON object that contain double quotes, such as
Copy Code code as follows:

{
"description": "25" "
}

If converted to a string form, the backslash is automatically added to "25\" and then passed to the rest API and saved to the MongoDB.

At this point, if you use the MongoDB shell to display the data, "25\", correct.

2. However, if you read this value in C + + driver, you get "25", so if you return it directly to the browser end, you can use Jquery.parsejson () to parse the error.

The C + + segment is serialized into a string that needs to be judged by "Replace with \".
Copy Code code as follows:

void string_to_json_string (std::string const& str, std::string & json_str) {
Std::stringstream SS;
for (size_t i = 0; i < str.length (); ++i) {
if (str[i] = = ' "') {
SS << ' \ \ ' << ' ';
} else {
SS << Str[i];
}
}
Json_str = Ss.str ();
}

3. If JavaScript calls Jquery.parsejson () on "25\", the backslash disappears and becomes "25". If you call Jquery.paresejson on the property value again, you will get an error.

JavaScript must write code to prevent errors:
Copy Code code as follows:

Removedoublequotes:function (str) {
return str.replace ("\" "," \\\ ");
},

This is the reincarnation of double quotes in JSON. Enough trouble, be careful.

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.