I'm fetching data from one api, and get STRING like:
[
{
"reason": "Invalid address",
"email": "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com"
},
{
"reason": "Invalid address",
"email": "dsfdsf"
},
]
Now I just do JSON.parse(data), then return. But, in Frontend side, when render the page, i got some weird characters like:
email: "jérômeläufer@com"
What I want is to escape the \u00c3\u00a9r... these stuff, just to show on the page like
email: "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com"
Or, encode these stuff, because I will use these email as a parameter to do some thing, but with these weird character, I can not get the same email.
is this possible to do that?
I've solved this issue, actually i'm fetching from sendgrid's api to get the invalid useremail list, api is
GET https://api.sendgrid.com/api/invalidemails.get.json?api_user=user&api_key=key
and got the return data like:
[
{
"reason": "Invalid address",
"email": "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com"
},
{
"reason": "Invalid address",
"email": "dsfdsf"
},
]
so first I treat this string "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com" using
decodeURIComponent(escape(string))
which will be shown correctly on the web page, then when I want to remove this email and let this email as a parameter to call the remove email api from sendgrid like:
POST(which is weriod,but it's official delete method) https://api.sendgrid.com/api/invalidemails.delete.json
the body is:
body:{
user: ..,
key: ...,
email:unescape(encodeURIComponent(email))
}
Then it works, so anybody knows why?
JSON.parse
is the right thing. The problem is with the content itself, not how you're handling it.
Really you need to talk to whoever is responsibly for the upstream API and get them to fix the mangling in their data. (Although, if the data is something you sent to them in the first place, it's still possible it's your fault at the other end.)
As a temporary measure you may be able to undo the mangling after parsing the JSON. If the mangling is that UTF-8 bytes have been misinterpreted as ISO-8859-1, there's an idiom that can be used to do a matching encode/decode cycle:
>>> decodeURIComponent(escape('jérômeläufer@com'));
'jérômeläufer@com'
(However, it's possible it's actually UTF-8 bytes misinterpreted as Windows code page 1252, in which case no equally-simple fix is available and the above would throw an execption. I can't which encoding it is from the above malformed string.)
Not that jérômeläufer@com
is a proper e-mail address either :-)