javascript – Unexpected identifier in json_encode output-ThrowExceptions

Exception or error:
echo '<script type="text/javascript">
            $(document).ready(function(){                    
                $("video").easyWebPlayer({ playlist:\''.json_encode($playlist).'\'
                                            });
            });
        </script>';

I am using the above code in a php file it produces following output.

'playlist:'[{"id":"0","url":"\/26beb276\/26beb276_0.mp4","thumb":"\/26beb276\/26beb276_0.jpg","title":"big buck test"}]'});});

It gives me error of Unexpected identifier .
How can I avoid this error.

How to solve:

Try below code:

<Script>
var playlist = '<?php echo $playlist; ?>';
var jsonplaylist = JSON.parse(playlist);
</Script> 
echo '<script type="text/javascript">
 $(document).ready(function(){ $("video").easyWebPlayer({ playlist:'+jsonplaylist+'});
</script>';

Answer:

solved
I used playlist:”‘.str_replace(“\””,”\'”,json_encode($playlist)).'” while json_encode()

and use
var jsonobj=JSON.parse(Playlist.replace(/\’/g, “\””));
at the receiver end.

Answer:

I had the same console error, the error was due to quotes

Before

<script>window.lang_arr="<?php echo json_encode($lang_arr);?></script>

Fix

<script>window.lang_arr=<?php echo json_encode($lang_arr);?></script>

It could help someone today :_)

Leave a Reply

Your email address will not be published. Required fields are marked *