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

时间:2015-12-22 04:39:59

标签: php arrays json field

我试图从JSON数组中获取一个特定的变量,似乎无法得到它。有人可以帮忙。

这是JSON

flatMap

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

我使用的是

{
  "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": "$1$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": "$1$PC$uWc8JwVNRbagWll2_RyDaw$idnV3v1jeVsL5g=="
    }
  ]
}

它似乎并不想玩游戏。

有人可以让我知道我哪里出错吗?

由于

2 个答案:

答案 0 :(得分:4)

$UserId = $user->data->ID; 是一个数组,所以你应该使用。

headerFields

答案 1 :(得分:3)

您还可以使用json_decode解码php中的json字符串

$r=json_decode('paste_json_code_here');
print_r($r->generic->headerFields);

希望这可能有所帮助。