这是我写的代码:
$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) }
对不起,我应该先检查一下。
注意:使用未定义的常量标签 - 假定"标签" C:''xampp''htdocs ''ai''sentiment.php 在第 11 行
在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 字符串格式。
$result不是
字符串。尝试使用 print_r($result)
找出字符串在对象中的存储位置。