Parsing Data

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Aug 14, 2014 1:13 am

Thu Aug 14, 2014 11:09 am

that looks like a json

so you have to decode it and use it like a normal object.

here's the code:

Code: Select all

	$json = json_decode($jsondata);
	if (empty($json)){
		echo "invalid json";
	// then you can access to variables 
	$cmd = $json->cmd;
	echo $cmd;
	$last_heard = $json->coreInfo->last_heard;
	echo $last_heard;
	// to check if a property exists ...
	if (isset($json->result)) {
		$result = $json->result;
		echo $result;
	// to check if a nested property exists u've to do write more code
	// @ char avoids php complains about non objects
	$prop = @$json->it->does->not->exists;
	if ($prop === NULL) {
		echo "it doesn't exists";
catch (Exception $ee) {
	echo "failed to parse json file";


Post Reply