javascript - Converting PHP Associative array to JSON object - spaces in value results in malformed JSON -
i build array in php , convert json handle onclick event , send file via ajax.
the problem of values in php array have spaces in them. seems breaking json object meaning ajax call failing.
here's snippet of code try , elaborate:
//php array looks this: array ( [card_id] => 1 [img_id] => 11 [card_name] => layout1retro_original [card_qty] => 1 [img_thumb] => albums/160915_e165/thumbs/011_cover-lp-cd_originaldpp_polaroid.jpg [img_hires] => [img_full] => albums/160915_e165/images/011_cover-lp-cd_originaldpp_polaroid.jpg [media] => retro [finish] => perl290 [size] => original [backing] => [can_crop] => [needs_to_be_cropped] => 1 [been_cropped] => [aspect_ratio] => 0 [offer_cmf] => [retro-name] => value ) the problem key => val pair retro-name. json breaking space between a , value.
i use json_encode convert array json object pass javascript: stripslashes(json_encode($array)).
then add html element data attribute can grab onclick event.
here's breaking in data attribute: [removed excess code clarity...] "retro-name":"a" value"}
notice has added closing " after a. breaks rest of json object ajax call fails.
how can fix this?
my best guess because json rendered in data attribute enclosed in double-quotes, common in html. instead of double-quotes, please try single-quotes. php outputs json double-quotes.
if not work, please can update question example of how data-attribute rendered json.
Comments
Post a Comment