Получить значения от Algolia JSON

У меня есть этот кусок 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;

ИЛИ ЖЕ Сделайте жизнь проще, расшифровав ее как массив

$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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector