1. If there are attributes in the JSON object that contain double quotes, such as
{
"description": "25""
}
If converted into string form, a backslash will be added automatically and become "25"", Then pass it to the REST API and save it to MongoDB.
If you use the MongoDB shell to display the data at this time, it will be "25"", which is correct.
2. But if you use the C driver to read this value, you will get "25"" , so if you return it directly to the browser and use jQuery.parseJSON() to parse it, an error will be reported.
When serializing segment C into a string, you need to make a judgment and replace "with".
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 After JavaScript calls jQuery.parseJSON() on "25"", the backslash has disappeared and changed to "25"". If jQuery.pareseJSON is called on the attribute value again, an error will occur again.
JavaScript must write code to prevent errors:
removeDoubleQuotes: function(str) {
return str.replace(""", "\"");
},
This is the reincarnation of double quotes in JSON. It's troublesome enough, be careful.