donglanying3855 2017-11-06 08:17
浏览 37

如何访问json文件字段[重复]

This question already has an answer here:

{
 "took" : 363,
"timed_out" : false,
"num_reduce_phases" : 15,
"_shards" : {
"total" : 7195,
"successful" : 7195,
"failed" : 0
 },
"hits" : {
"total" : 35672,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"2" : {
  "doc_count_error_upper_bound" : 54,
  "sum_other_doc_count" : 1463,
  "buckets" : [
    {
      "key" : "ふるさと納税",
      "doc_count" : 30376
    }
   ]
 }
 }
}

This is the json file . I need to access the doc_count field.

I am trying to do it like this

$trend_words = json_decode($file);
$aggregations = $trend_words->aggregations;
$buckets = $aggregations->{2}->buckets->{0};

But its not working. Can someone help.

</div>
  • 写回答

4条回答 默认 最新

  • dongne1560 2017-11-06 08:22
    关注
    $trend_words = json_decode($file,true);
    $aggregations = $trend_words['aggregations']['2']['buckets'][0]['doc_count'];
    print_r($aggregations)
    

    You should use ,true otherwise you will get an stdclass object and not an array you want.

    评论

报告相同问题?