通过php解析json(LARAVEL)

zyfwsgd6  于 2023-08-08  发布在  PHP
关注(0)|答案(1)|浏览(136)

我不知道问题出在哪里。我已经试过了所有可能的方法,但是使用json的经验是不够的。请帮我找出我做错了什么。我将非常感谢正确的函数代码。

错误

未定义的数组键“gm_bk_id”{“userId”:1,“exception”:“[object](ErrorException(code:0):未定义的数组键“gm_bk_id”

验证码:

public function parse()
    {
        $data = json_decode(file_get_contents(public_path() . "/telnet.json"), true);
        $slots = $data['locator']['groups'];

        foreach($slots as $slot) {
            Slot::create([
                'game_id'       => $slot['gm_bk_id'],
                'title'         => $slot['gm_title'],
                'alias'         => $slot['gm_url'],
                'provider'      => $slot['gr_title'],
                'provider_type' => 'TELNET',
                'image'         => $slot['icons']
            ]);
        }

        return redirect()->back()->withSuccess('The list updated');
    }

字符串

需要从中获取信息的JSON文件

{
   "success":true,
   "api":"ls-thy-by-yhr-id-get",
   "locator":{
      "ico_baseurl":"\/sdfsgre\/icons\/",
      "groups":[
         {
            "games":[
               {
                  "gm_is_board":false,
                  "gm_m_w":1280,
                  "gm_ln":20,
                  "gm_is_copy":true,
                  "gm_url":"Oeprjkg",
                  "gm_is_retro":false,
                  "gm_bk_id":1017,
                  "gm_d_w":1280,
                  "icons":[
                     {
                        "ic_h":221,
                        "ic_w":300,
                        "ic_name":"oeprjkg_300_221.jpg"
                     },
                     {
                        "ic_h":135,
                        "ic_w":170,
                        "ic_name":"oeprjkg_170_135.jpg"
                     },
                     {
                        "ic_h":101,
                        "ic_w":150,
                        "ic_name":"oeprjkg_150_101.jpg"
                     },
                     {
                        "ic_h":220,
                        "ic_w":220,
                        "ic_name":"oeprjkg_220_220.jpg"
                     }
                  ],
                  "gm_title":"Oeprjkg",
                  "gm_m_h":720,
                  "gm_is_fs":true,
                  "gm_is_mobile":true,
                  "gm_is_pc":true,
                  "gm_new":false,
                  "gm_d_h":720
               },
               {
                  "gm_is_board":false,
                  "gm_m_w":1280,
                  "gm_ln":10,
                  "gm_is_copy":true,
                  "gm_url":"Tdfhhdf",
                  "gm_is_retro":false,
                  "gm_bk_id":1008,
                  "gm_d_w":1280,
                  "icons":[
                     {
                        "ic_h":221,
                        "ic_w":300,
                        "ic_name":"Tdfhhdf_300_221.jpg"
                     },
                     {
                        "ic_h":135,
                        "ic_w":170,
                        "ic_name":"Tdfhhdf_170_135.jpg"
                     },
                     {
                        "ic_h":101,
                        "ic_w":150,
                        "ic_name":"Tdfhhdf_150_101.jpg"
                     },
                     {
                        "ic_h":220,
                        "ic_w":220,
                        "ic_name":"Tdfhhdf_220_220.jpg"
                     }
                  ],
                  "gm_title":"Tdfhhdf",
                  "gm_m_h":720,
                  "gm_is_fs":true,
                  "gm_is_mobile":true,
                  "gm_is_pc":true,
                  "gm_new":false,
                  "gm_d_h":720
               }
            ],
            "gr_title":"Tjtyuk",
            "gr_id":12
         },
         {
            "games":[
               {
                  "gm_is_board":false,
                  "gm_m_w":1280,
                  "gm_ln":4096,
                  "gm_is_copy":true,
                  "gm_url":"Idofjio",
                  "gm_is_retro":false,
                  "gm_bk_id":2001,
                  "gm_d_w":1280,
                  "icons":[
                     {
                        "ic_h":221,
                        "ic_w":300,
                        "ic_name":"Idofjio_300_221.jpg"
                     },
                     {
                        "ic_h":135,
                        "ic_w":170,
                        "ic_name":"Idofjio_170_135.jpg"
                     },
                     {
                        "ic_h":101,
                        "ic_w":150,
                        "ic_name":"Idofjio_150_101.jpg"
                     },
                     {
                        "ic_h":220,
                        "ic_w":220,
                        "ic_name":"Idofjio_220_220.jpg"
                     }
                  ],
                  "gm_title":"Idofjio",
                  "gm_m_h":720,
                  "gm_is_fs":true,
                  "gm_is_mobile":true,
                  "gm_is_pc":true,
                  "gm_new":false,
                  "gm_d_h":720
               },
               {
                  "gm_is_board":false,
                  "gm_m_w":1280,
                  "gm_ln":243,
                  "gm_is_copy":true,
                  "gm_url":"Egdfhfgh",
                  "gm_is_retro":false,
                  "gm_bk_id":2000,
                  "gm_d_w":1280,
                  "icons":[
                     {
                        "ic_h":221,
                        "ic_w":300,
                        "ic_name":"Egdfhfgh_300_221.jpg"
                     },
                     {
                        "ic_h":135,
                        "ic_w":170,
                        "ic_name":"Egdfhfgh_170_135.jpg"
                     },
                     {
                        "ic_h":101,
                        "ic_w":150,
                        "ic_name":"Egdfhfgh_150_101.jpg"
                     },
                     {
                        "ic_h":220,
                        "ic_w":220,
                        "ic_name":"Egdfhfgh_220_220.jpg"
                     }
                  ],
                  "gm_title":"Egdfhfgh",
                  "gm_m_h":720,
                  "gm_is_fs":true,
                  "gm_is_mobile":true,
                  "gm_is_pc":true,
                  "gm_new":false,
                  "gm_d_h":720
               }
            ],
            "gr_title":"Egdfhfgh",
            "gr_id":19
         }
      ]
   }
}


h4cxqtbf

h4cxqtbf1#

“gm_bk_id”键嵌套在“games”数组中,“games”数组嵌套在“groups”数组中。您需要正确地遍历这些嵌套数组以访问“gm_bk_id”键。

public function parse()
{
    $data = json_decode(file_get_contents(public_path() . "/telnet.json"), true);
    $groups = $data['locator']['groups'];

    foreach ($groups as $group) {
        $games = $group['games'];
        foreach ($games as $game) {
            Slot::create([
                'game_id'       => $game['gm_bk_id'],
                'title'         => $game['gm_title'],
                'alias'         => $game['gm_url'],
                'provider'      => $group['gr_title'],
                'provider_type' => 'TELNET',
                'image'         => $game['icons'][0]['ic_name'] // You might need to adjust this depending on how you want to store the image
            ]);
        }
    }

    return redirect()->back()->withSuccess('The list updated');
}

字符串

相关问题