尝试使用php从json获取数据时,不会返回任何结果
下午好 我目前在用PHP从json文件获取数据时遇到问题 我现在得到的是尝试使用php从json获取数据时,不会返回任何结果,php,json,Php,Json,下午好 我目前在用PHP从json文件获取数据时遇到问题 我现在得到的是 $jsondecoded = json_decode('[{"chat":{"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}', true); echo ($jsondecoded[0]->chat); 如你所见。我正在尝试获取聊天信息。但是,当我尝试这样做时,它没有任何回应。我一直在试图
$jsondecoded = json_decode('[{"chat":{"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}', true);
echo ($jsondecoded[0]->chat);
如你所见。我正在尝试获取聊天信息。但是,当我尝试这样做时,它没有任何回应。我一直在试图找出原因,但遗憾的是我找不到。我一定错过了什么,但我不知道我错过了什么
高级版谢谢。三个错误:
1) json结尾处缺少]
2) 您正在使用json_decode的“array assoc”选项,因此它不会返回对象
3) 您不能回显聊天对象
试试这个:
$jsondecoded = json_decode('[{"chat":{"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}]');
echo ($jsondecoded[0]->chat->username);
另外,请记住,您使用了true
作为json_decode
的第二个参数,这意味着您要将结果转换为关联数组,请删除该true
或将访问权限更改为:
var_dump($jsondecoded[0]['chat']);
您的json在json字符串的末尾添加']'无效,并且php中的打印数组使用print_r函数[0]'用于获取第一个元素,['chat']用于获取聊天键的值
$jsondecoded = json_decode('[{"chat":{"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}]', true);
print_r ($jsondecoded[0]['chat']);
输出将像
Array
(
[username] => RobloxProtectorKing
[message] => :slender me
[time] => 2018-03-20 01:56:12
)
下面是我的一个模板示例。希望能有帮助
<?php
/* Status Codes
return 0 = Nothing to Update (n/a)
return 1 = Successful Insert Query
return 2 = Database Connection refused
return 3 = MySQL Query Error OR Wrong URL Parameters */
/* Disable Warnings so that we can return ONLY what we want through echo. */
mysqli_report(MYSQLI_REPORT_STRICT);
// First get raw POST input
$raw_post = file_get_contents('php://input');
// Run through url_decode..
$url_decoded = urldecode($raw_post);
// Run through json_decode...
// false to allow for reference to oject. eg. $column->name instead of $column["name"] in the foreach.
$json_decoded = json_decode($url_decoded, false);
$pk_line_item_id = (strlen($json_decoded[0]->value) > 0 ? $json_decoded[0]->value : null);
// INCLUDE DB CONNECTION STRING
include 'php_pdo_mysql_connect.php';
// SQL INSERT query...
$stmt = $link->prepare(" SELECT * FROM tbl_xyz ");
//$stmt->bindParam(':pk_line_item_id', $pk_line_item_id, PDO::PARAM_INT); // INT
// Execute this SQL statement.
$stmt->execute();
// Fetch & Populate the single returned in var $resultSet.
$resultSet = $stmt->fetchAll();
// Returns an array indexed by column number as returned in your result set, starting at column 0.
// https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.swg.im.dbclient.php.doc/doc/t0023505.html
// If the search record was found, populate it on the html table.
if (($resultSet !== false) && ($stmt->rowCount() > 0)) {
// Set JSON headers
header('Content-type:application/json;charset=utf-8');
// Encode entire $resultSet array to JSON.
$json_encoded = json_encode($resultSet);
// Ready to return as JSON to client side JQuery / Java Script...
echo $json_encoded;
}
?>
无效的json,结尾缺少]
。遗憾的是,这不是问题所在。我修复了它,同样的事情也发生了。4)echo
是一个构造,不需要用括号括起来。哇。真不敢相信我会错过这么简单的事情。谢谢迪奥尼的帮助,将来会非常有用的。
<?php
/* Status Codes
return 0 = Nothing to Update (n/a)
return 1 = Successful Insert Query
return 2 = Database Connection refused
return 3 = MySQL Query Error OR Wrong URL Parameters */
/* Disable Warnings so that we can return ONLY what we want through echo. */
mysqli_report(MYSQLI_REPORT_STRICT);
// First get raw POST input
$raw_post = file_get_contents('php://input');
// Run through url_decode..
$url_decoded = urldecode($raw_post);
// Run through json_decode...
// false to allow for reference to oject. eg. $column->name instead of $column["name"] in the foreach.
$json_decoded = json_decode($url_decoded, false);
$pk_line_item_id = (strlen($json_decoded[0]->value) > 0 ? $json_decoded[0]->value : null);
// INCLUDE DB CONNECTION STRING
include 'php_pdo_mysql_connect.php';
// SQL INSERT query...
$stmt = $link->prepare(" SELECT * FROM tbl_xyz ");
//$stmt->bindParam(':pk_line_item_id', $pk_line_item_id, PDO::PARAM_INT); // INT
// Execute this SQL statement.
$stmt->execute();
// Fetch & Populate the single returned in var $resultSet.
$resultSet = $stmt->fetchAll();
// Returns an array indexed by column number as returned in your result set, starting at column 0.
// https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.swg.im.dbclient.php.doc/doc/t0023505.html
// If the search record was found, populate it on the html table.
if (($resultSet !== false) && ($stmt->rowCount() > 0)) {
// Set JSON headers
header('Content-type:application/json;charset=utf-8');
// Encode entire $resultSet array to JSON.
$json_encoded = json_encode($resultSet);
// Ready to return as JSON to client side JQuery / Java Script...
echo $json_encoded;
}
?>