4

What is it about ?

I am adding the elements in an array and finally trying to convert that array into 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',
 ));

The above gives below mentioned output

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 for converting the array into Json.

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

and Finally json gives below output. Which can be seen in 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 it gives " in the json data ?

asked Dec 20, 2015 at 9:26
1

1 Answer 1

7

The JSON string uses quotes to show where a value starts and ends in basic terms.

Give

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

a go. This will echo out an unescaped string for json.

answered Dec 20, 2015 at 9:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.