2016-10-04 31 views
0

我有這塊JSON,我需要FACETS。從Algolia JSON獲取值

我已成功地獲得了 「nbHits」 但我與得到

$res = "the json below" 

$res = json_encode($res); 

$obj = json_decode($res); 

print $obj->{'nbHits'}; /// 1116 

我已經試過

print $obj['facets'] 

print $obj['facets'][0] 

我是 「面」 的奮鬥錯過簡單的東西,任何幫助表示讚賞。

的JSON

{ 
    "hits": [{ 
    "contactId": 986838846, 
    "email": "sdfghjhgfdfgh", 
    "numOpens": 0, 
    "numPageViews": 0, 
    "numClicks": 0, 
    "numForwards": 0, 
    "numEstimatedForwards": 0, 
    "numReplies": 0, 
    "dateSent": "2016-09-23T13:56:54.52", 
    "dateFirstOpened": "1970-01-01T00:00:00", 
    "dateLastOpened": "1970-01-01T00:00:00", 
    "firstOpenIp": "", 
    "unsubscribed": "false", 
    "softBounced": "false", 
    "hardBounced": "false", 
    "dept": "nil", 
    "appt": "no", 
    "hasClicked": "no", 
    "hasOpened": "no", 
    "hasReplied": "no", 
    "kId": "nil", 
    "salesExec": "Charlie", 
    "sold": "no", 
    "calledCust": "no", 
    "objectID": "986838846", 
    "_highlightResult": { 
    "email": { 
     "value": "dfghgfdghj", 
     "matchLevel": "none", 
     "matchedWords": [] 
     }, 
    "dept": { 
    "value": "nil", 
    "matchLevel": "none", 
    "matchedWords": [] 
    }, 
    "appt": { 
    "value": "no", 
    "matchLevel": "none", 
    "matchedWords": [] 
    }, 
    "hasClicked": { 
    "value": "no", 
    "matchLevel": "none", 
    "matchedWords": [] 
    }, 
    "hasOpened": { 
    "value": "no", 
    "matchLevel": "none", 
    "matchedWords": [] 
    }, 
    "hasReplied": { 
    "value": "no", 
    "matchLevel": "none", 
    "matchedWords": [] 
    }, 
    "salesExec": { 
    "value": "Charlie", 
    "matchLevel": "none", 
    "matchedWords": [] 
    }, 
    "sold": { 
    "value": "no", 
    "matchLevel": "none", 
    "matchedWords": [] 
    }, 
    "calledCust": { 
    "value": "no", 
    "matchLevel": "none", 
    "matchedWords": [] 
    } 
} 
    }], 
    "nbHits": 1116, 
    "page": 0, 
    "nbPages": 1000, 
    "hitsPerPage": 1, 
    "processingTimeMS": 1, 
    "facets": { 
    "appt": { 
    "no": 994 
    }, 
    "dept": { 
    "nil": 994 
    }, 
    "sold": { 
    "no": 994 
    }, 
"hasOpened": { 
    "no": 629, 
    "yes": 365 
}, 
"salesExec": { 
    "nil": 394, 
    "Alfa": 100, 
    "Bravo": 100, 
    "Charlie": 100, 
    "Delta": 100, 
    "Echo": 100, 
    "Foxtrot": 100, 
    "Hotel": 100 
}, 
"hasClicked": { 
    "no": 975, 
    "yes": 19 
}, 
"hasReplied": { 
    "no": 989, 
    "yes": 5 
} 
}, 
    "exhaustiveFacetsCount": true, 
    "query": "", 
    "params":  "facets=%5B%22hasOpened%22%2C%22dept%22%2C%22appt%22%2C%22sold%22%2C%22salesExec%22%2C%22hasClicked%22%2C%22hasReplied%22%5D&hitsPerPage=1&query=" 
} 

回答

0

結果已是JSON編碼格式的,爲什麼你又編碼呢?

去抓方面的沒有嘗試這種方式$obj->facets->appt->no

$res = 'Your JSON goes here'; 
//-----Don't do again json_encode() again here 
$obj = json_decode($res); 
print '<pre>'; 
print_r($obj); 
print '</pre>'; 
echo $obj->facets->appt->no; 

OR使生活更輕鬆通過解碼它作爲一個陣列

$array = json_decode($res,1); //see second parameter here 
print '<pre>'; 
print_r($array); 
print '</pre>'; 
echo $array['facets']['appt']['no']; 
echo $array['facets']['hasOpened']['yes']; 
+0

很好解釋 – Jason

+0

@Jason,很高興如果它爲你工作 –