Javascript 使用转义字符解析 JSON

Javascript Parsing JSON with Escape Characters

所以我有一个从 Android Pay API 调用返回的 JSON 对象,我需要提取数据并进行处理。问题是它包含必须保留的反斜杠。我已经查看了很多关于此的主题,但似乎没有任何效果。如何从下面的数据中解析出 key/value 对并保留数据中的反斜杠?

{"ephemeralPublicKey":"BE1Ai3\/HjtEon0JAz+9jK8GmF9vzmXpDACcpjsXDD5EkP7HmhdXrNjVMdi58itYNGH6HoZvlOY65qvHWRwS0pu8=","encryptedMessage":"BIrMosJ9dQSeCL0ImJPhml3++grM6AvW9qLkTkp6I\/gt1q+JsFB9rm9Ndzdjd7UuN\/RJm6osMHd1UDIi84VlQljJKMQnRQKrQrqx15jlNOBlGrALOTY3TQIYyWOvMB8I\/Kr6yFduvy+b2IVR+3GPDjI4J3PKHd+xPLh4WvXhmBckda54T3vVW\/2Toxi02IC1w\/n5AkTlKfem6XTvw9sC","tag":"GEyeDyeNrXn3T2GYHFOL4WeQLBRBVt7PCKmr9OfJa3M="}

我看到代码有效,你的问题有什么意义?

你应该像这样转义反斜杠字符。

var str = '{"ephemeralPublicKey":"BE1Ai3\/HjtEon0JA"}';//...string response from api 
var obj = JSON.parse(str.replace('\/','\\/'));
obj.ephemeralPublicKey

我想通了。

var data = {"ephemeralPublicKey":"BE1Ai3\/HjtEon0JAz+9jK8GmF9vzmXpDACcpjsXDD5EkP7HmhdXrNjVMdi58itYNGH6HoZvlOY65qvHWRwS0pu8=","encryptedMessage":"BIrMosJ9dQSeCL0ImJPhml3++grM6AvW9qLkTkp6I\/gt1q+JsFB9rm9Ndzdjd7UuN\/RJm6osMHd1UDIi84VlQljJKMQnRQKrQrqx15jlNOBlGrALOTY3TQIYyWOvMB8I\/Kr6yFduvy+b2IVR+3GPDjI4J3PKHd+xPLh4WvXhmBckda54T3vVW\/2Toxi02IC1w\/n5AkTlKfem6XTvw9sC","tag":"GEyeDyeNrXn3T2GYHFOL4WeQLBRBVt7PCKmr9OfJa3M="};

var obj = JSON.parse(data.replace(/\/g, '\\'));
console.log("Value: " + obj.ephemeralPublicKey);