I'm currently trying to unserialize data given from a HTML data attribute. The value comes as a serialized string from my DB.
To get the value I'm doing a readout out of my DB. After that I'm replacing any " by ' to prevent problems with the opening and closing tag inside HTML:
<span data-value="<?= str_replace( '"', "'", $obj->value ) ?>"
Here is an example how it finally looks like:
<span data-value="a:2:{i:0;a:2:{s:5:'issue';s:4:'Test';s:10:'estimation';s:1:'5';}i:1;a:2:{s:5:'issue';s:4:'Impl';s:10:'estimation';s:1:'5';}}"></span>
My plan is now to receive the value in JS and transform it back to an array so that I can loop over it. I already tried JSON.parse() but that failed. Do you have any idea how to get this done?
Update:
Because some of you told me that my replace breaks my string, this here is the original one with an example:
jQuery( document ).ready( function ( $ ) {
let attr = $("span").attr("data-test");
console.log(JSON.parse(attr));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span data-test='a:2:{i:0;a:2:{s:5:"issue";s:4:"Test";s:10:"estimation";s:1:"5";}i:1;a:2:{s:5:"issue";s:4:"Impl";s:10:"estimation";s:1:"5";}}'></span>
data-valueattribute.