Is there any function like json.parse or stringify which creates a valid json format.
I have string in this format "{'EncryptionType':'aa', 'EncryptionKey':'bb', 'EncryptionDone' :'cc'}"
and I am trying to convert to valid json format.
like "{"EncryptionType":"aa"", "EncryptionKey":"bb", "EncryptionDone" :"cc"}"
var details = "{'EncryptionType':'aa', 'EncryptionKey':'bb', 'EncryptionDone' :'cc'}"
var updatedDetails = JSON.stringify(details);
alert(updatedDetails);
string FinalResult = "{"EncryptionType":"aa"", "EncryptionKey":"bb", "EncryptionDone" :"cc"}"
I tested and its works;
var details = "{'EncryptionType':'aa', 'EncryptionKey':'bb', 'EncryptionDone' :'cc'}"
var updatedDetails = eval('('+details+')');
alert(updatedDetails.EncryptionType)
Your details
string is already in valid JSON format; JSON can use either single or double quotes for key/value pairs. Paste it into jsonlint.com and see for yourself.
If you're getting errors of some sort, it's for a different reason.
Note: your FinalResult
variable isn't valid JSON as it has two double quotes after aa
.
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.