使用php遍历json时获取空属性错误

时间:2016-06-07 04:50:59

标签: php json solr

我正在使用Apache Solr Engine根据输入关键字向用户提供建议。

我有以下格式的json响应。

{
    "suggest": {
        "mySuggester": {
            "A Ga": {
                "numFound": 2,
                "suggestions": [
                {
                    "term": "<b>A Ga</b>me of Thrones",
                    "weight": 0,
                    "payload": "Book"
                },
                {
                    "term": "<b>A Ga</b>teway to Heaven",
                    "weight": 0,
                    "payload": "Book"
                }]
            }
        }
    }
}

密钥A Ga是用户输入,即基于此密钥,提供建议。现在Solr返回这个JSON,它存储在php的$response变量中,密钥A Ga包含在变量$inp中。

在我的代码中,有一行是

$noOfSuggestions =  $response->suggest->mySuggester->$inp->numFound;

我在此行收到无法访问空属性错误。我对$inp的使用是否正确?

谢谢!

编辑:这是var_dump($response);

object(Apache_Solr_Response)#60 (5) { ["_response":protected]=> object(Apache_Solr_HttpTransport_Response)#50 (5) { ["_statusCode":"Apache_Solr_HttpTransport_Response":private]=> int(200) ["_statusMessage":"Apache_Solr_HttpTransport_Response":private]=> string(2) "OK" ["_mimeType":"Apache_Solr_HttpTransport_Response":private]=> string(10) "text/plain" ["_encoding":"Apache_Solr_HttpTransport_Response":private]=> string(5) "UTF-8" ["_responseBody":"Apache_Solr_HttpTransport_Response":private]=> string(511) "{"responseHeader":{"status":0,"QTime":2},"suggest":{"mySuggester":{"A Ga":{"numFound":2,"suggestions":[{"term":"A Game of Thrones","weight":0,"payload":"Book"},{"term":"A Gateway to Heaven","weight":0,"payload":"Book"}]}}}} " } ["_isParsed":protected]=> bool(false) ["_parsedData":protected]=> NULL ["_createDocuments":protected]=> bool(true) ["_collapseSingleValueArrays":protected]=> bool(true) }

0 个答案:

没有答案