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

Source: Internet
Author: User
Tags json mongodb

  If an attribute in the JSON object contains double quotes when converted to a string, the backslash is automatically added, please see this article in detail

1. If there are attributes in the JSON object that contain double quotes, such as the     code as follows: {  "description": "" " }    if converted to a string form, will automatically add a backslash, into" 25 "", then passed to the rest API and saved to the MongoDB.     If you use the MongoDB shell to display the data at this time, "25", correct.     2. But if you read this value in C + + driver, you get "25", so if you return it directly to the browser end, you can parse it with Jquery.parsejson () to make an error. The     C + + segment is serialized as a string and needs to be judged by the "Replace with" .  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:    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.