How can I insert special characters inside a JSON? For example if I have:
{
"a": "Hola mundo"
}
and I want to put the word "world" in quotes. I tried escaping them and with the HTML code: "I show the string in a web page, but neither of the two options worked, I don't know if it's because of the library that loads it or it's something inherent to the use of JSON.
Edited:
Escaped quotes:\"
Didn't properly phrase the question. I see that my problem is with the ngx-translate library for Angular2 , not with the JSON itself. This library stores the translations in a JSON file, but it has not allowed me to insert double quotes inside them as I have indicated before.
According to the standard that handles JSON.org for the use of strings or
String
, the values must start with one"
to use a valid character, except for the double quote, the backslash must be used\
and after it the control character that you need obviously ending with"
Some valid JSON
Now what does that mean
\u
, what is it for? simple is used for special characters called eñes accents among others in unicode format .An example of when we use Unicode is when we send the responses via GET the parameter we transform it to unicode with the encodeURIComponent command
If you can, you should only use a backslash (
\
) in front of the quotes, it will not be valid if it does not have opening and closing quotes.So:
I leave you these two solutions:
Otherwise you can try:
And if you experience any other problems use the JSON.parse function, once you have your object correctly stored in your script.
In JSON, as with any other string, there are escape strings that allow you to insert special characters such as: line break, tabulation, accents, among others... And for this case in particular, the backslach must be placed followed by the character to be taken as a string (\")...