通过服务器获取JSON数据的问题?

时间:2010-06-19 07:31:15

标签: php json

我正在使用Tropo Web API,从聊天应用程序获取结果,我将我的JSON响应重定向到我的服务器端PHP文件(http://myhost.in/vj/app3.php?tropo-engine=json),JSON响应正确,但我无法获取使用PHP的数据, JSON响应如下..

{
    "result": {
        "actions":
            [
                {
                    "attempts": 2, 
                    "concept": "mumbai", 
                    "confidence": 100, 
                    "disposition": "SUCCESS", 
                    "interpretation": "mumbai", 
                    "name": "city", 
                    "utterance": "mumbai", 
                    "value": "mumbai", 
                    "xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><result grammar=\"1@6ac61bfd.vxmlgrammar\"><interpretation grammar=\"1@6ac61bfd.vxmlgrammar\" confidence=\"100\"><instance>mumbai<\/instance><input mode=\"voice\" confidence=\"100\" timestamp-start=\"1970-01-01T00:00:00.000\" timestamp-end=\"1970-01-01T00:00:00.000\">mumbai<extensions><word-confidence> 100 <\/word-confidence><\/extensions><\/input><extensions><probability> 0 <\/probability><nl-probability> 0 <\/nl-probability><necessary-word-confidence> 0 <\/necessary-word-confidence><\/extensions><\/interpretation><\/result>\r\n\r\n"
                }, 
                {
                    "attempts": 1, 
                    "concept": "cricket", 
                    "confidence": 100, 
                    "disposition": "SUCCESS", 
                    "interpretation": "cricket", 
                    "name": "sports", 
                    "utterance": "cricket", 
                    "value": "cricket", 
                    "xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><result grammar=\"2@6ac61bfd.vxmlgrammar\"><interpretation grammar=\"2@6ac61bfd.vxmlgrammar\" confidence=\"100\"><instance>cricket<\/instance><input mode=\"voice\" confidence=\"100\" timestamp-start=\"1970-01-01T00:00:00.000\" timestamp-end=\"1970-01-01T00:00:00.000\">cricket<extensions><word-confidence> 100 <\/word-confidence><\/extensions><\/input><extensions><probability> 0 <\/probability><nl-probability> 0 <\/nl-probability><necessary-word-confidence> 0 <\/necessary-word-confidence><\/extensions><\/interpretation><\/result>\r\n\r\n"
                }
            ], 
        "callId": "b546e03db1ccfd2f0e8c58d970539501", 
        "complete": true, 
        "error": null, 
        "sequence": 1, 
        "sessionDuration": 11, 
        "sessionId": "ed6abc73b9c434385ea8cd8004c9ed0c", 
        "state": "ANSWERED"
    }
}

我正在使用的PHP代码...

$json = file_get_contents("php://input");
$result = json_decode($json);
$value =$result->result->actions->value;

但我在变量$ value中得到null。如何获得城市和体育的价值...... ??

1 个答案:

答案 0 :(得分:3)

这是因为$result->result->actions是一个数组。试试$result->result->actions[0]->value