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
请检查我的下面的代码,请让我知道我正在做的错误,我只想解码json并在浏览器上打印为数组
<?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.
我尝试了这个json字符串在线解码(http://jsonviewer.stack.hu/),它的工作正常,所以json字符串没有问题。任何帮助将是欣赏。
2 个解决方案
#1
2
<?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)));
?>
#2
0
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.
问题来自你所拥有的json格式..你有一些','需要被删除,这就是为什么json_decode()无法完成他的工作。此函数抛出错误异常但您应该执行一些技巧来查看错误。您可以使用此代码查看错误。
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应该是这样的。
$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"
}
]
}
]
}';
#1
2
<?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)));
?>
#2
0
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.
问题来自你所拥有的json格式..你有一些','需要被删除,这就是为什么json_decode()无法完成他的工作。此函数抛出错误异常但您应该执行一些技巧来查看错误。您可以使用此代码查看错误。
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应该是这样的。
$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"
}
]
}
]
}';