I have a piece of JSON that I'm parsing with php. I need to get one of the pieces of data out of it.
Here's the output of the json when I do a print_r:
Array ( [deviceId] => 07a9727e-3fe5-4f44-9765-134388241f39 [programId] => 3895 [serviceId] => 19977 [createdAt] => 2013年12月12日T07:19:04.466Z [updatedAt] => 2013年12月12日T07:19:04.466Z [objectId] => 7TxmL2GiXq )
Here's my code trying to extract deviceId:
$objectData = json_decode($data, true);
print_r($objectData);
$deviceId = $objectData->deviceId;
$deviceId is coming back empty.
Any help would be appreciated. Thanks.
1 Answer 1
Do this:
$deviceId = $objectData['deviceId'];
You are using the optional second parameter TRUE in your json_decode call, which converts it into an associative array instead of an object.
Alternatively:
$objectData = json_decode($data);
$deviceId = $objectData->deviceId; // Works
answered Dec 12, 2013 at 19:21
jszobody
29k6 gold badges66 silver badges76 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
armedstar
awesome! this works! I knew it was something small I was overlooking. Thanks so much.
lang-php