i cant for the life of me figure out why i cant get this JSON to work
this is how it comes back from api
"{"errorCode":5,"errorDescription":"Unknown account"}"
i have tried all of the following to get "Unknown account"
$error = $result['errorCode":5,"errorDescription'];
$error = $result['errorCode'];
$error = $result['errorDescription'];
i would be ok even just trying for error code 5, so i can do a check to further the script if account is made or not.
any ideas what i am missing?
2 Answers 2
try this:
$json = '{"errorCode":5,"errorDescription":"Unknown account"}';
$arr = json_decode($json, true);
$error = $arr['errorDescription'];
In this case you take the json and parse it into the function json_decode of php, wit true value as second parameter It return an associative error where you can get your value by key.
2 Comments
It's actually a simple matter as there are already builtin functions in PHP for handling JSON. Those are json_encode and json_decode.
$result='{"errorCode":5,"errorDescription":"Unknown account"}';
var_dump($result);
$json=json_decode($result);
var_dump($json);
echo "errorCode={$json->errorCode}<br>";
echo "errorDescription={$json->errorDescription}<br>";
Try the above. Note that json_decode can convert a string into either an object (default) or an associative array.
$json2=json_decode($result,TRUE);
var_dump($json2);
echo "errorCode=".$json2['errorCode']."<br>";
echo "errorDescription=".$json2['errorDescription']."<br>";
json_decodeit, and it's probably$result->errorCodeafter that. Useprint_rto examine its structure.