简体   繁体   中英

javascript to get substring of recordNumber

I have a string as below and i wanted to extract the recordNumber value. For ex: i need to extract the value like PTL-HPCAN-2022-00157

{"Request":"{\"header\":{\"action\":\"SubmitRecordAndCalculateFees\",\"username\":\"PUBLICUSER\",\"trxId\":\"2c348091-3f1c-4ccb-b854-92cd5d164565\",\"language\":\"EN\"},\"body\":{\"recordNumber\":\"PTL-HPCAN-2022-00157\"}}"}

Try below code:

var inputString = {"Request":"{\"header\":{\"action\":\"SubmitRecordAndCalculateFees\",\"username\":\"PUBLICUSER\",\"trxId\":\"2c348091-3f1c-4ccb-b854-92cd5d164565\",\"language\":\"EN\"},\"body\":{\"recordNumber\":\"PTL-HPCAN-2022-00157\"}}"}

console.log(JSON.parse(inputString.Request).body.recordNumber)

Your string seems to be an invalid string, please check the below code on how to extract the desired output!

 const str = "{\"header\":{\"action\":\"SubmitRecordAndCalculateFees\",\"username\":\"PUBLICUSER\",\"trxId\":\"2c348091-3f1c-4ccb-b854-92cd5d164565\",\"language\":\"EN\"},\"body\":{\"recordNumber\":\"PTL-HPCAN-2022-00157\"}}"; const obj = JSON.parse(str); console.log(obj.body.recordNumber);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM