0

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?

asked Jun 10, 2017 at 20:41
1
  • 2
    Show us the code where you fetch the response. Chances are you first have to json_decode it, and it's probably $result->errorCode after that. Use print_r to examine its structure. Commented Jun 10, 2017 at 20:42

2 Answers 2

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.

DEMO

answered Jun 10, 2017 at 20:42
Sign up to request clarification or add additional context in comments.

2 Comments

omg i had forgotten this "$result = json_decode($cexecute,true);"
It can be happen, glad to help you! Accept the answer to help other guys with your same problem if this has solved your. Thanks
1

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>";
answered Jun 10, 2017 at 20:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.