繁体   English   中英

无法访问JSON编码数据

[英]JSON Encoded data not accessible

我使用模型从数据库查询数据。

echo $user = User::whereRaw('username = ? and password = ?', array($username,$password))->get();

输出为JSON格式

[{"id":1,"name":"Abhijith","username":"abhi","created_at":"2014-07-31 20:07:35","updated_at":"2014-07-31 20:07:35"}]

但是,当我尝试回显单个字段时,出现索引未找到错误。

echo $user->id; //Gives an error saying the index is not found

您需要养成查看结构而不是假设的习惯。 另外,您需要对字符串进行json_decode 然后使用print_r查看结构:

$result = json_decode($user);
echo $result[0]->id;

或(我认为PHP> = 5.4.0):

echo json_decode($user)[0]->id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM