Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode returns " [duplicate]

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],
like image 898
Orion Avatar asked Nov 30 '15 07:11

Orion


People also ask

What does json_encode return?

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.

What is json_encode function?

The json_encode() function is used to encode a value to JSON format.

What json_decode () function will return?

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.

What is json_encode and json_decode?

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.


1 Answers

If you use blade, try {!! json_encode($sm_names) !!}

like image 109
KAndy Avatar answered Sep 20 '22 18:09

KAndy