Page 1 of 1

API Tutorial... Decoding JSON

Posted: Thu Aug 18, 2011 12:09 am
by phpkid01
hey guys...

i have a question... you know the errors parameters in the JSON well its encoded in another array...

i was wondering how you decode that array aswell... to show the independant error messages as im kinda stuck...
init.inc.php:
session_start();
$login = surfcms::validate_credentials($_POST['username'], $_POST['password'], $_POST['key']);
$session = $login['session'];
$username = $login['username'];
$errors = $login['errors'];
if($session==1){
	$_SESSION['site'] = "scms_" . $username;
}else{
	echo $errors;	
}
Cheers :)

Re: API Tutorial... Decoding JSON

Posted: Thu Aug 18, 2011 11:00 am
by jacek
You can use [url=http://php.netjson_decode/json_decode]json_decode[/url], just pass it the encoded string and you will get the original array back.

Re: API Tutorial... Decoding JSON

Posted: Thu Aug 18, 2011 2:19 pm
by phpkid01
so i should use:
session_start();
$login = surfcms::validate_credentials($_POST['username'], $_POST['password'], $_POST['key']);
$session = $login['session'];
$username = $login['username'];
$errors = json_decode($login['errors']);
if($session==1){
	$_SESSION['site'] = "scms_" . $username;
}else{
	foreach($errors as $error){
		echo $error;
	}	
}

Re: API Tutorial... Decoding JSON

Posted: Thu Aug 18, 2011 5:07 pm
by jacek
No, the function handles the decoding, you just use the result.

You should just be able to use $login['errors'] directly.

Re: API Tutorial... Decoding JSON

Posted: Thu Aug 18, 2011 9:13 pm
by phpkid01
it only returns "Array" as the result... :/

Re: API Tutorial... Decoding JSON

Posted: Sat Aug 20, 2011 12:10 pm
by jacek
phpkid01 wrote:it only returns "Array" as the result... :/
you can't just echo an array. try doing a print_r($something) to see the format of the data you are getting.