Parsing Data

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

Re: Parsing Data

Post by kladrian » 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


try{
	$json = json_decode($jsondata);
	if (empty($json)){
		echo "invalid json";
		return;
	}
	// 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";
}


---
kladrian

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests