2015-12-22 129 views
2

我想從JSON數組中獲取特定變量,並且似乎無法得到它。有人可以請幫忙。PHP嘗試從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": "$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==" 
    } 
    ] 
} 

,我試圖讓HeaderFields - 只是>值

我使用的線是

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

它 - >值和secondaryFields似乎不想玩遊戲。

有人能讓我知道我要去哪裏嗎?

感謝

回答

4

headerFields是一個數組,所以你應該使用。

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

嗨@bansi謝謝你。完美工作。 –

3

你也可以使用json_decode以JSON字符串在PHP

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

希望這可以幫助解碼。