PHP 尝试从 JSON 字符串中获取特定变量

PHP Trying to get a specific variable from a JSON string

我正在尝试从 JSON 数组中获取特定变量,但似乎无法获取。有人可以帮忙吗

这是JSON

{
  "associatedStoreIdentifiers": [
    578661564
  ],
  "associatedApps": [
    {
      "title": "Psbook",
      "idGooglePlay": "com..app"
    }
  ],
  "locations": [
    {
      "longitude": -122.3748889,
      "latitude": 37.6189722
    },
    {
      "longitude": -122.03118,
      "latitude": 37.33182
    }
  ],
  "barcode": {
    "format": "PKBarcodeFormatQR",
    "messageEncoding": "iso-8859-1",
    "altText": "0497 6880 9072 8",
    "message": "$PC$uWc8JwVNRbagWll2_RyDaw$idnV3v1jeVsL5g=="
  },
  "logoText": "",
  "foregroundColor": "rgb(0,0,0)",
  "backgroundColor": "rgb(255,255,255)",
  "generic": {
    "headerFields": [
      {
        "label": "Floor",
        "value": "LEVEL 15",
        "key": "Floor"
      }
    ],
    "primaryFields": [
      {
        "label": "Building",
        "value": " Melbourne",
        "key": "member"
      }
    ],
    "secondaryFields": [
      {
        "label": "Location",
        "value": "FEMALE TOILET",
        "key": "subtitle"
      }
    ],
    "auxiliaryFields": [],
    "backFields": [
      {
        "key": "-poweredby",
        "value": "Find out more and create your own passes at:\nhttp://www..com.au",
        "label": "Powered by "
      },
      {
        "key": "legalnotice",
        "label": "Legal Notice",
        "value": "This pass has been created by THE PASS ISSUER."
      }
    ]
  },
  "serialNumber": "b9673c27-054d-45b6-a05a-5976fd1c836b",
  "passTypeIdentifier": "pass.com",
  "formatVersion": 1,
  "description": "",
  "organizationName": "Group",
  "teamIdentifier": "34V8SZHXRM",
  "authenticationToken": "f018753e-d059-49ca-ac2c-362a3de8cff3",
  "webServiceURL": "https://pass.center/s",
  "barcodes": [
    {
      "format": "PKBarcodeFormatQR",
      "messageEncoding": "iso-8859-1",
      "altText": "0497 6880 9072 8",
      "message": "$PC$uWc8JwVNRbagWll2_RyDaw$idnV3v1jeVsL5g=="
    }
  ]
}

我正在尝试获取 HeaderFields -> Value 和 secondaryFields -> Value

我使用的线路是

$level = $json->generic->headerFields->value

而且就是不想玩游戏

有人可以告诉我我哪里出错了吗?

谢谢

headerFields 是一个数组,所以你应该使用。

$level = $json->generic->headerFields[0]->value;
                                  //  ^ the index you want

您还可以使用 json_decode 来解码 php

中的 json 字符串
$r=json_decode('paste_json_code_here');
print_r($r->generic->headerFields);

希望这可能有所帮助。