0

I have the following string which was the result of running a stringify command in node:

"{\"packageName\":\"com.something.something\",\"productId\":\"product_credit_50\",\"purchaseTime\":1484696888772,\"purchaseState\":0,\"purchaseToken\":\"xxxxxxxxxxxxxxxxxxxxxxx.XX-X1Xxx0QZlCo1hk7cEPqocpIpttvxSPCjwf5cqvraOTXAv7k9nglTSCdpWjAuqZ2w1rtRJTYbJ6483YG09huOCQesngxGe_gD_f83mrMsu7Yw5tEioPE1vyORGMVykhykRYn-vaE8TTQ3CgLxFafPJ7cEJwEl59w\"}"

The command I used to get the string is as follows:

var tmpreceipt = JSON.stringify(googleReceiptData);

I then executed the following command to remove the escaped characters:

var receipt = tmpreceipt.replace("\\\"", "\"");

The problem i have is that it only removes the first "\". The result of receipt is:

"{"packageName\":\"com.something.something\",\"productId\":\"product_credit_50\",\"purchaseTime\":1484696888772,\"purchaseState\":0,\"purchaseToken\":\"xxxxxxxxxxxxxxxxxxxxxxx.XX-X1Xxx0QZlCo1hk7cEPqocpIpttvxSPCjwf5cqvraOTXAv7k9nglTSCdpWjAuqZ2w1rtRJTYbJ6483YG09huOCQesngxGe_gD_f83mrMsu7Yw5tEioPE1vyORGMVykhykRYn-vaE8TTQ3CgLxFafPJ7cEJwEl59w\"}"

What I am doing wrong? Thanks in advance.

leo c
  • 663
  • 3
  • 8
  • 19
  • It seems you should not apply `JSON.stringify` at all, as looks like you already had a JSON string. – trincot Jan 20 '17 at 06:31
  • If you're just asking about how to replace all instances of `\"` with `"` instead of just the first, [this explains how to replace all occurrences of a string in JavaScript](http://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Tashi Jan 20 '17 at 06:34
  • i am getting a different problem if I don't use stringify. do you see any problems with the code I did? I was expecting that the replace should be able to remove the escaping. – leo c Jan 20 '17 at 06:35
  • @Tashi thanks. That's what I was trying to do. – leo c Jan 20 '17 at 07:56

0 Answers0