【问题标题】:Error in Json Data : Laravel 5.1Json 数据错误:Laravel 5.1
【发布时间】:2015-12-20 09:26:15
【问题描述】:

这是关于什么的?

我在一个数组中添加元素,最后尝试将该数组转换为 Json。下面是代码。

$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',
            ));

上面给出了下面提到的输出

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

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

)

以下是用于将数组转换为 Json 的代码。

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

最后 json 给出以下输出。可以在查看源代码中看到

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;}];

问题

为什么它在 json 数据中给出 "?

【问题讨论】:

标签: php laravel-5 laravel-5.1


【解决方案1】:

JSON 字符串使用引号以基本术语显示值的​​开始和结束位置。

给予

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

去吧。这将为 json 回显一个未转义的字符串。

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 2017-09-23
    • 1970-01-01
    • 2015-10-04
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2017-01-12
    相关资源
    最近更新 更多