【问题标题】:Json starting with [ and when using json_encode it starts with " in magento2 using php7.1Json 以 [ 开头,使用 json_encode 时,它​​在 magento2 中以 " 开头,使用 php7.1
【发布时间】:2019-12-04 16:40:00
【问题描述】:

我希望我的 json 以 { 开头,但如果使用 json_encode 它会被转换为字符串,我在 ubuntu 上使用 php7.1 并使用 magento 2.3

这就是我用下面的代码得到的,我不想要'['

[
    {
        "success": "true",
        "data": {
            "mainimages": [
                {

这是我的代码

$response = array(
    array(
        "success" => "true",
        "data" => $alldata,
        "newarrivalheading" => "NEW ARRIVALS",
        "instagramheading" => "CELEBS IN LULU",
        "specialpriceheading" => "SPECIAL PRICES",
        "editorwishlistheading" => "EDITOR'S WISHLIST",
        "stylehighlightheading" => "STYLE HIGHLIGHTS",
        "styletagline" => "#Looks to swipe right",
        "newarrivalindex" => 3,
        "instagramindex" => 9,
        "editorwishlistviewall" => "",
        "sliderimage" => $sliderimage
    )
);      
return $response;

这就是我想要的

 {
        "success": "true",
        "data": {
            "mainimages": [
                {

【问题讨论】:

  • 太棒了,json_encode($response[0])?或者干脆去掉外面的array()
  • 您需要养成accepting answers 的习惯,它可以帮助您解决问题。您将获得积分,并鼓励其他人帮助您。
  • @Jay Blanchard 我仍然面临问题,所以一旦解决,我会接受答案。

标签: php arrays json php-7.1 magento-2.3


【解决方案1】:

所以像这样删除不必要的外部数组

$alldata = [1,2,3,4];
$sliderimage = ['xz.jpg','ab.png'];

$response = array(
               "success" => "true",
               "data" => $alldata,
               "newarrivalheading" => "NEW ARRIVALS",
               "instagramheading" => "CELEBS IN LULU",
               "specialpriceheading" => "SPECIAL PRICES",
               "editorwishlistheading" => "EDITOR'S WISHLIST",
               "stylehighlightheading" => "STYLE HIGHLIGHTS",
               "styletagline" => "#Looks to swipe right",
               "newarrivalindex" => 3,
               "instagramindex" => 9,
               "editorwishlistviewall" => "",
               "sliderimage" => $sliderimage
        );
echo json_encode($response);

结果

    {
    "success": "true",
    "data": [
        1,
        2,
        3,
        4
    ],
    "newarrivalheading": "NEW ARRIVALS",
    "instagramheading": "CELEBS IN LULU",
    "specialpriceheading": "SPECIAL PRICES",
    "editorwishlistheading": "EDITOR'S WISHLIST",
    "stylehighlightheading": "STYLE HIGHLIGHTS",
    "styletagline": "#Looks to swipe right",
    "newarrivalindex": 3,
    "instagramindex": 9,
    "editorwishlistviewall": "",
    "sliderimage": [
        "xz.jpg",
        "ab.png"
    ]
}

【讨论】:

  • 嗨@RiggsFolly 感谢您的回复,$sliderimage 也在数组中,所以现在我得到了一些东西 "styletagline": "#Looks to swipe right", "newarrivalindex": 3, " instagramindex": 9, "editorwishlistviewall": "", "sliderimage": "" }[]
  • 除非我错过了什么,那应该没什么区别,我改变了答案,把$sliderimage = ['xz.jpg','ab.png'];变成了一个数组
  • 我的 json 末尾有 []。
  • 我的代码没有,所以很明显你正在做一些我没有做的事情
  • 您的代码 returns $response 并因此在其他地方将其转换为 JSON。回复这个回复后怎么办??这可能是您的问题所在
猜你喜欢
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
相关资源
最近更新 更多