这是我写的代码:
$result = $textProc->sentiment($text);
$json_a = json_decode($result, true);
echo $json_a[label];$result存储JSON数据的位置。
但是,它会返回错误:
Warning: json_decode() expects parameter 1 to be string, object given in C:\xampp
\htdocs\ai\sentiment.php on line 9
Notice: Use of undefined constant label - assumed 'label' in C:\xampp\htdocs
\ai\sentiment.php on line 11解决方案:这是var_dump($result)的输出:
object(stdClass)#2 (2) { ["value"]=> float(0.63882080795918) ["sent"]=> int(1) } 对不起,我应该先检查一下的。
发布于 2012-04-22 19:31:17
标签注意事项:未定义常量标签的使用-假定在第11行的C:\xampp\htdocs \ai\sentiment.php中使用‘
’
On echo $json_a[label];标签引用了一个不存在的常量。
要引用关联数组中的元素,请执行以下操作。
echo $json_a['label'];警告: json_decode()要求参数1为字符串,对象位于C:\xampp \htdocs\ai\sentiment.php的第9行
接下来,在$result = $textProc->sentiment($text);上,函数不会返回字符串。做一个var_dump($result)确保,它返回的是json 字符串格式。
发布于 2012-04-22 19:30:29
$result不是字符串。尝试使用print_r($result)找出字符串在对象中的存储位置。
https://stackoverflow.com/questions/10267543
复制相似问题