在php中解码多级json

时间:2016-09-09 10:39:51

标签: php json curl

我试图在以下响应中访问不同的结果,但似乎无法使其工作:

{
  "id": "REGNOHERE",
  "registration": "REGNOHERE",
  "vehicle": {
    "dvla": {
      "make": "AUDI",
      "model": "A4 SE TDI 6SP",
      "regDate": "2008-09-29",
      "v5cDate": "2011-10-27",
      "vin": "WAUZZZ8K99A099266",
      "colour": "BLUE",
      "source": "UK",
      "exported": "1",
      "exportedDate": "2015-12-09",
      "imported": null,
      "importedNi": null,
      "importedDate": null,
      "body": "4 DOOR SALOON",
      "bodyClass": "CAR",
      "seatingCapacity": 5,
      "wheelplan": "2 AXLE RIGID BODY",
      "co2": 139,
      "cc": 1968,
      "engineNumber": "CAG 120938",
      "engineCode": "2.0 TDI E4",
      "fuel": "HEAVY OIL",
      "maxPower": 105,
      "maxTechMass": 2010
    }

我使用以下方法解码响应,并尝试访问其中的数据

$data = json_decode($response, TRUE);
$carmake['vehicle']['dvla']['make'];
我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

$carmake不存在。从json_decode创建的数组是$data。要分配$carmake,您可以这样做:

$carmake = $data['vehicle']['dvla']['make'];
echo $carmake;
// output..
// AUDI