从Algolia JSON获取价值

时间:2016-10-04 15:56:35

标签: php json algolia

我有这件JSON,我需要FACETS。

我设法得到了" nbHits"但我正在努力获得" facets"

$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="
}

1 个答案:

答案 0 :(得分:0)

结果已经是json编码格式,为什么你再次编码呢?

要抓住 facets 上的值,请尝试$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'];