如何将json数组解码为php数组JSON_DECODE

时间:2016-12-27 09:16:17

标签: php json cakephp

我用json_encode对php数组进行编码并将其存储到db中,当我尝试将其解码为php数组时,我得到null。

这是我的php数组:

'Etablissment' => array(
    'id' => '79',
    'telephone' => '0620000000',
    'nom' => 'RESTo',
    'nbmaxpersonnes' => '20',
    'hoursreservation' => '{"Lundi":{"matin":{"h_debut":"0630 ","h_fin":"0830 "},"midi":{"h_debut":"0730 ","h_fin":"1300 "}},"Mardi":{"matin":{"h_debut":"0700 ","h_fin":"0500 "},"midi":{"h_debut":"1530 ","h_fin":"1400 "}}}'
),

我想解码hoursreservation字段,所以我json_decode($etablissement['Etablissment']['hoursreservation']),我使用cakephp。 以下解决方案对我不起作用: php json decodejson decode in php

2 个答案:

答案 0 :(得分:1)

<?php
$etablissement = [
'Etablissment' => array(
    'id' => '79',
    'telephone' => '0620000000',
    'nom' => 'RESTo',
    'nbmaxpersonnes' => '20',
    'hoursreservation' => '{"Lundi":{"matin":{"h_debut":"0630 ","h_fin":"0830 "},"midi":{"h_debut":"0730 ","h_fin":"1300 "}},"Mardi":{"matin":{"h_debut":"0700 ","h_fin":"0500 "},"midi":{"h_debut":"1530 ","h_fin":"1400 "}}}'
)];

$d = json_decode($etablissement['Etablissment']['hoursreservation'], true);
$e = json_last_error();

var_dump($d);

答案 1 :(得分:0)

尝试

$etablissement = array('Etablissment' => array(
     'id' => '79',
     'telephone' => '0620000000',
     'nom' => 'RESTo',
     'nbmaxpersonnes' => '20',
     'hoursreservation' => '{"Lundi":{"matin":{"h_debut":"0630 ","h_fin":"0830 "},"midi":{"h_debut":"0730 ","h_fin":"1300 "}},"Mardi":{"matin":{"h_debut":"0700 ","h_fin":"0500 "},"midi":{"h_debut":"1530 ","h_fin":"1400 "}}}'
));

echo json_decode($etablissement['Etablissment']['hoursreservation'],true);

你会得到数组,没有问题。