Error in Json Data: Laravel 5.1

What does it mean?

I am adding elements to an array and finally trying to convert this array to Json. Below is the code.

$SubCategoryList = array();

array_push($SubCategoryList, 
            array(
                'SubCategoryID' =>  1,
                'FirstName'   =>  'First Name',
                'LastName'    =>  'Last Name',
            ));
array_push($SubCategoryList, 
            array(
                'SubCategoryID' =>  2,
                'FirstName'   =>  'First Name2',
                'LastName'    =>  'Last Name2',
            ));

Above conclusion below

Array
(
    [0] => Array
        (
            [SubCategoryID] => 1
            [FirstName] => First Name
            [LastName] => Last Name
        )

    [1] => Array
        (
            [SubCategoryID] => 2
            [FirstName] => First Name2
            [LastName] => Last Name2
        )

)

Below is the code used to convert the array to Json.

<script>
     var subCategoriesList = {{ json_encode($SubCategoryList) }};
</script>

and finally json prints below. What can be seen in the view source

var subCategoriesList = [{&quot;SubCategoryID&quot;:1,&quot;FirstName&quot;:
&quot;First Name&quot;,&quot;LastName&quot;:&quot;Last Name&quot;},
{&quot;SubCategoryID&quot;:2,&quot;FirstName&quot;:&quot;First Name2&quot;,
&quot;LastName&quot;:&quot;Last Name2&quot;}];

Question

Why does it give "in json data?

+4
source share
1 answer

The JSON string uses quotation marks to indicate where the value begins and ends in basic terms.

Give

 var subCategoriesList = {!! $SubCategoryList !!};

a go. This will cause json not to be saved.

+7
source

Source: https://habr.com/ru/post/1620970/


All Articles