Renegade Rob
Renegade Rob

Reputation: 403

PHP Trying to get a specific variable from a JSON string

I am trying to get a specific variable from a JSON array, and cant seem to get it. Could someone please help.

Here is the 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": "$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=="
    }
  ]
}

And I am trying to get HeaderFields -> Value and secondaryFields -> Value

The line I am using is

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

and it just does not seem to want to play game.

Could someone please let me know where I am going wrong?

Thanks

Upvotes: 2

Views: 70

Answers (2)

bansi
bansi

Reputation: 57042

headerFields is an array so you should use.

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

Upvotes: 4

Abbas
Abbas

Reputation: 412

you can also use json_decode to decode json string in php

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

Hope this might help.

Upvotes: 3

Related Questions