2012-04-22 43 views
0

这是我写的代码:这些PHP脚本在阅读JSON时出了什么问题?

$result = $textProc->sentiment($text); 
$json_a = json_decode($result, true); 
echo $json_a[label]; 

其中$结果存储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($结果)输出:

object(stdClass)#2 (2) { ["value"]=> float(0.63882080795918) ["sent"]=> int(1) } 

对不起,我应该检查这第一。

+0

如何声明$结果? – 2012-04-22 11:32:22

+0

发布var_dump($ result)的输出;' – Starx 2012-04-22 11:32:34

+0

尝试'var_dump($ result)'并查看它返回的结果。您可能只能将其转换为字符串。 – cmbuckley 2012-04-22 11:33:05

回答

2

Notice: Use of undefined constant label - assumed 'label' in C:\xampp\htdocs \ai\sentiment.php on line 11

echo $json_a[label];标签是指不存在的常数。

要引用关联数组中的元素,请按如下所示进行操作。

echo $json_a['label']; 

Warning: json_decode() expects parameter 1 to be string, object given in C:\xampp \htdocs\ai\sentiment.php on line 9

接下来,$result = $textProc->sentiment($text);,该函数没有返回一个字符串。做一个var_dump($result)来保证,它返回json 字符串格式。

1

$结果不是字符串。尝试使用print_r($result)找出字符串存储在对象中的哪个位置。

+0

帮助!谢谢你! – Newbie 2012-04-22 11:41:05