Please check my below code and please let me know ehat wrong I am doing , I just want to decode the json and print on the browser as array
<?php
$json = '{
"Token" : "xxx-xxx-xxx",
"ID": "1",
"Recipients": [
{
"Recipient_ID": "XX",
"From_Name": "XXX",
"From_Email": "XXX",
"To_Name": "XXX",
"To_Email": "XXX",
"Subject": "XXX",
"Message": "XXX",
"Attachments": [
{
"File_Name": "XXX",
"File_Path": "XXX",
}
],
}
],
}';
$input = $json;
print_r(json_decode(stripslashes($input)));
?>
I tried this json string to decode online (http://jsonviewer.stack.hu/) and its working fine so no issue with json string. Any help would be appreciate.
<?php
$json = '{
"Token": "xxx-xxx-xxx",
"ID": "1",
"Recipients": [{
"Recipient_ID": "XX",
"From_Name": "XXX",
"From_Email": "XXX",
"To_Name": "XXX",
"To_Email": "XXX",
"Subject": "XXX",
"Message": "XXX",
"Attachments": [{
"File_Name": "XXX",
"File_Path": "XXX"
}]
}]
}';
$input = $json;
var_dump(json_decode(stripslashes($input)));
?>
the problem stand from the json format you have.. You have some ',' that needs to be removed and that's why the json_decode() can't do his job. This function throws an error exception but you should do a little trick to view the error. You can use this code to view the error.
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
And you json should be like this.
$json = '{
"Token" : "xxx-xxx-xxx",
"ID": "1",
"Recipients": [
{
"Recipient_ID": "XX",
"From_Name": "XXX",
"From_Email": "XXX",
"To_Name": "XXX",
"To_Email": "XXX",
"Subject": "XXX",
"Message": "XXX",
"Attachments": [
{
"File_Name": "XXX",
"File_Path": "XXX"
}
]
}
]
}';