I am trying to build a chart with Chart.js using laravel. The json_encode function returns the string with & quot; instead of " that Chart.js cannot recognize. Tried the htmlspecialchars_decode() with no luck. Is there any other workaround ? Please help.
This is the laravel code
labels: {{ htmlspecialchars_decode(json_encode($sm_names)) }},
datasets: [{
data: {{ str_replace('"','',json_encode($sm_totals))}},
and this is how it is rendered in javascript:
labels: ["John","Mercy ","Gary"],
datasets: [{
data: [425000,470000,10000],
The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.
The json_encode() function is used to encode a value to JSON format.
The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.
JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.
If you use blade, try {!! json_encode($sm_names) !!}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With