如何在PHP中制作不同结构的JSON

我正在从 2 个表中检索数据:1) 主页横幅和 2) 热门交易。之后,我从中制作 REST API。但是我在 json 中获取数据是这样的:


 [

    {

        "homuri": "/Home/Banner/activity.png"

    },

    {

        "homuri": "/Home/Banner/fitness.png"

    },   

    {

        "hotdeal": "/Home/HotDeals/hotdeal1.png"

    },

    {

        "hotdeal": "/Home/HotDeals/hotdeal2.png"

    },

    {

        "hotdeal": "/Home/HotDeals/hotdeal3.png"

    },


]

但我想要我的 json 结构是这样的:


[

        "banner":{

            "homuri": "/Home/Banner/activity.png",

            "homuri": "/Home/Banner/fitness.png"

        },   

        "hotdeals":{

            "hotdeal": "/Home/HotDeals/hotdeal1.png",

            "hotdeal": "/Home/HotDeals/hotdeal2.png",

            "hotdeal": "/Home/HotDeals/hotdeal3.png",

        },    

]

如何做到这一点?以及如何在 HTML 中访问所有这些值?下面我附上我的代码:


<?php


    include('dbconn.php');

    $request=$_SERVER['REQUEST_METHOD'];


    $data=array();

    switch($request)

    {

        case 'GET':

            response(getData());                        

    }


    function getData()

    {

        global $conn;

        @$col=$_GET['col'];


        $query=mysqli_query($conn,"select strHomeBannerUri as homuri from tblhomebannerdetails");


        while($row=mysqli_fetch_assoc($query))

        {

            $data[]=array("homuri"=>$row['homuri']);            

        }


        if($col=="pop")

        {

            $query=mysqli_query($conn,"select nHotrDealImageUri as hotdeal from tblhotdealimages");


        while($row=mysqli_fetch_assoc($query))

        {

            $data[]=array("hotdeal"=>$row['hotdeal']);          

        }


        }           

        return $data;           

    }



    function response($data)

    {

        echo json_encode($data);

    }

?>


智慧大石
浏览 161回答 1
1回答

繁星coding

您希望 JSON 看起来的方式将不起作用 - 您不能在同一“嵌套级别”中多次使用一个键。function getData(){&nbsp; &nbsp; global $conn;&nbsp; &nbsp; @$col=$_GET['col'];&nbsp; &nbsp; $query=mysqli_query($conn,"select strHomeBannerUri as homuri from tblhomebannerdetails");&nbsp; &nbsp; while($row=mysqli_fetch_assoc($query))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data['homeuri'][]= $row['homuri'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; if($col=="pop")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $query=mysqli_query($conn,"select nHotrDealImageUri as hotdeal from tblhotdealimages");&nbsp; &nbsp; while($row=mysqli_fetch_assoc($query))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data['hotdeals'][]= $row['hotdeal'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; return $data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}可以解决这个问题 - 你的逻辑有一些问题,你应该检查自己如何在 PHP 中编写和修改数组。完成后,请检查 JSON 以及如何构建 JSON 对象/列表。上面的代码应该返回类似{&nbsp; &nbsp; "banner":[&nbsp; &nbsp; &nbsp; &nbsp; "/Home/Banner/activity.png",&nbsp; &nbsp; &nbsp; &nbsp; "/Home/Banner/fitness.png"&nbsp; &nbsp; ],&nbsp; &nbsp;&nbsp; &nbsp; "hotdeals":[&nbsp; &nbsp; &nbsp; &nbsp; "/Home/HotDeals/hotdeal1.png",&nbsp; &nbsp; &nbsp; &nbsp; "/Home/HotDeals/hotdeal2.png",&nbsp; &nbsp; &nbsp; &nbsp; "/Home/HotDeals/hotdeal3.png"&nbsp; &nbsp; ],&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP