Closed KaKi87 closed 5 years ago
Can you give an example?
Use JSON.stringify()
to generate JSON (and take care of escaping quotes in strings).
take care of escaping quotes in strings
That's exactly what I want to automate :)
Ok, I guess your issue is resolved then? :)
No because I didn't found out how to automate string quote escaping
I'm confused. I just told you that JSON.stringify
does that for you.
JSON.parse(JSON.stringify(val))
returns val
It does indeed!
Again, can you give an example of what you're trying to do? Provide example input, desired output, and code you've tried so far.
Here is a JSFiddle example : https://jsfiddle.net/KaKi87/56pbLtuf/
How is the content inserted into the <pre>
? Are you doing that on the server side?
No, this is an example. The original is a real JSON response, fetched using NodeJS request. But, this fiddle have the same behavior as in my script. The point is that I can't auto-escape quotes inside JSON strings.
The original is a real JSON response, fetched using NodeJS request.
If it's a proper JSON response, then JSON.parse
would succeed, making it different from the demo you shared.
I'm still struggling to understand what you mean.
Okay then try to parse this : https://trigedasleng.net/api/api?action=search&query=dei%20de
Works for me:
(async () => {
const response = await fetch('https://trigedasleng.net/api/api?action=search&query=dei%20de');
const data = await response.json();
console.log('Result:', data);
})();
// Result: {...}
Okay but did you decoded the HTML entities ?
To decode HTML entities in a string, use he.decode(string)
.
(async () => {
const response = await fetch('https://trigedasleng.net/api/api?action=search&query=dei%20de');
const data = await response.json();
data.words = data.words.map((word) => {
word.translation = he.decode(word.translation);
return word;
});
console.log('Result:', data);
})();
// Result: {...}
So, I must decode property by property ??
Yeah. There's no way he
or any other library can know which values need decoding and which do not; it depends on the use case.
All I need is to automatically escape the "
character
It seems to me that you could use the string.replace method to replace any instance of " with \" in your server strings (if that is truly "all you need"). The he library is used to create and remove HTML entities, not to use \ to escape characters in strings.
The HTML code I wanna convert is formatted as JSON.
My JSON string contains HTML entities inside properties values, and especially
"
, which is the encoded character for"
.So, I need to escape the previously converted characters in JSON, which means adding a backslash (
\
) to them.Thanks