一个比较复杂的 json ,用 json_decode() 转数组会 null

andyzu

这是我的 json,肯定合法:

{
    "categories": [
        {
            "id": 1,
            "title": "上衣类",
            "products": [
                {
                    "id": 8,
                    "title": "运动服",
                    "nums": 2,
                    "price": 60.00
                },
                {
                        "id": 10,
                    "title": "夹克衫",
                    "nums": 2,
                    "price": 60.00
                }
            ],
            "total_nums": 4,
            "total_price": 120.00
        },
        {
            "id": 5,
            "title": "汽车类",
            "products": [
                {
                    "id": 63,
                    "title": "布艺两件套",
                    "nums": 5,
                    "price": 250.00
                },
                {
                        "id": 65,
                    "title": "布艺七件套",
                    "nums": 1,
                    "price": 200.00
                }
            ],
            "total_nums": 6,
            "total_price": 450.00
        }
    ],
    "count_price": 570.00
}

代码如下:

public function postWashOrderinfo(Request $request){
        $param = $request->all();
        // 进行参数校验
        $validate = new PostWashOrderinfoValidate();
        if (!$validate->check($param)) {
            return json($validate->getError());
        }

        $orderinfo = json_decode($param['orderinfo'],true);
        return $orderinfo;
    }

$orderinfo = json_decode($param['orderinfo'],true);
不管加不加 true 就是 null。
截图

509 2 0
2个回答

admin

var_dump ($param['orderinfo']),贴出下

  • andyzu 2023-11-20

    string(1122) "{
    "categories": [
    {
    "id": 1,
    "title": "上衣类",
    "products": [
    {
    "id": 8,
    "title": "运动服",
    "nums": 2,
    "price": 60.00
    },
    {
    "id": 10,
    "title": "夹克衫",
    "nums": 2,
    "price": 60.00
    }
    ],
    "total_nums": 4,
    "total_price": 120.00
    },
    {
    "id": 5,
    "title": "汽车类",
    "products": [
    {
    "id": 63,
    "title": "布艺两件套",
    "nums": 5,
    "price": 250.00
    },
    {
    "id": 65,
    "title": "布艺七件套",
    "nums": 1,
    "price": 200.00
    }
    ],
    "total_nums": 6,
    "total_price": 450.00
    }
    ],
    "count_price": 570.00
    }"

  • turbo 2023-11-20

    你可以试下放在body里传过去

  • liziyu 2023-11-20

    好像是这么回调

小W

post请求最好不要使用QueryString传参。

  • andyzu 2023-11-20

    嗯,最后改成 body 就OK 了

×
🔝