I want to convert below unicode string to JSON Object.
var str = '{"method_title":"Bank. Transfer","instructions":"Account Name: Sriram Me Co.,Ltd.\r\n----------------------------------------------\r\n- Furnin Commercial Bank\r\nAccount: 111-111-111\r\n----------------------------------------------\r\n- LIKSA Bank\r\nAccount: 111-111-111r\n----------------------------------------------\r\n\r\nAfter you have made bank transfer, please kindly visit \"PAYMENT\" to submit your payment detail."}';
below is the approach that I have followed
var myEscapedJSONString = decodeURIComponent(str);
console.log(JSON.parse(myEscapedJSONString));
and I am getting the below error:
Uncaught SyntaxError: Unexpected token in JSON at position 82
decodeURIComponent
is out of business here.
To use JSON.parse
, the format must be correct.
So here, you must first remove line breaks with something like .replace(/(\r\n|\n|\r)/gm,"")
.
Then the next problem is "PAYMENT"
, double quote cannot appears here, so you can remove with .replace(/"PAYMENT"/g, 'PAYMENT');
const str = '{"method_title":"Bank. Transfer","instructions":"Account Name: Sriram Me Co.,Ltd.\r\n----------------------------------------------\r\n- Furnin Commercial Bank\r\nAccount: 111-111-111\r\n----------------------------------------------\r\n- LIKSA Bank\r\nAccount: 111-111-111r\n----------------------------------------------\r\n\r\nAfter you have made bank transfer, please kindly visit \"PAYMENT\" to submit your payment detail."}'; const cleaned = str.replace(/(\r\n|\n|\r)/gm, "").replace(/"PAYMENT"/g, 'PAYMENT'); console.log(JSON.parse(cleaned));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.