【问题标题】:Getting a specific data from a json file using php使用 php 从 json 文件中获取特定数据
【发布时间】:2020-09-12 19:23:07
【问题描述】:

您好,我有一个 json 文件,我想从中提取一些数据 该文件如下所示:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 4.0201406,
    "hits" : [
      {
        "_index" : "catalogue",
        "_type" : "_doc",
        "_id" : "p95iEXIBfVAlCluoT3sT",
        "_score" : 4.0201406,
        "_source" : {
          "categorie" : "Bonbons ",
          "marque" : "TIC TAC",
          "nom" : "bonbon duo TIC TAC"
        }
      },
      {
        "_index" : "catalogue",
        "_type" : "_doc",
        "_id" : "od5iEXIBfVAlCluoT3sT",
        "_score" : 3.6080353,
        "_source" : {
          "categorie" : "Bonbons",
          "marque" : "TIC TAC",
          "nom" : "Bonbons duo de fraises TIC TAC"
        }
      }
    ]
  }
}

这是 elasticserch 查询的结果,我的问题是如何使用 php 从该文件中检索类别、商标、标称及其值? 我尝试将此结果转换为数组,我使用了 json_decode() 但我总是得到 null 然后我使用 Service_json() 并解决了问题。 在那之后,我得到了这个结果,但现在我很难得到一些特定的数据,比如类别、标称、品牌和它们的值,我得到了空结果。 这是我的 php 代码:

array(1) {
  ["hits"]=>
  object(stdClass)#3 (1) {
    ["hits"]=>
    array(7) {
      [0]=>
      object(stdClass)#4 (2) {
        ["_id"]=>
        string(20) "kN5iEXIBfVAlCluoT3sT"
        ["_source"]=>
        object(stdClass)#5 (3) {
          ["categorie"]=>
          string(7) "Bonbons"
          ["marque"]=>
          string(6) "MENTOS"
          ["nom"]=>
          string(29) "Bonbons parfums fruits MENTOS"
        }
      }
      [1]=>
      object(stdClass)#6 (2) {
        ["_id"]=>
        string(20) "kd5iEXIBfVAlCluoT3sT"
        ["_source"]=>
        object(stdClass)#7 (3) {
          ["categorie"]=>
          string(7) "Bonbons"
          ["marque"]=>
          string(6) "MENTOS"
          ["nom"]=>
          string(21) "Bonbons menthe MENTOS"
        }
      }
      [2]=>
      object(stdClass)#8 (2) {
        ["_id"]=>
        string(20) "kt5iEXIBfVAlCluoT3sT"
        ["_source"]=>
        object(stdClass)#9 (3) {
          ["categorie"]=>
          string(7) "Bonbons"
          ["marque"]=>
          string(6) "MENTOS"
          ["nom"]=>
          string(37) "Bonbons caramel/chocolat blanc MENTOS"
        }
      }
      [3]=>
      object(stdClass)#10 (2) {
        ["_id"]=>
        string(20) "k95iEXIBfVAlCluoT3sT"
        ["_source"]=>
        object(stdClass)#11 (3) {
          ["categorie"]=>
          string(7) "Bonbons"
          ["marque"]=>
          string(6) "Mentos"
          ["nom"]=>
          string(31) "Bonbons caramel/chocolat MENTOS"
        }
      }
      [4]=>
      object(stdClass)#12 (2) {
        ["_id"]=>
        string(20) "lN5iEXIBfVAlCluoT3sT"
        ["_source"]=>
        object(stdClass)#13 (3) {
          ["categorie"]=>
          string(7) "Bonbons"
          ["marque"]=>
          string(6) "MENTOS"
          ["nom"]=>
          string(28) "Bonbons menthe sucres MENTOS"
        }
      }
      [5]=>
      object(stdClass)#14 (2) {
        ["_id"]=>
        string(20) "ld5iEXIBfVAlCluoT3sT"
        ["_source"]=>
        object(stdClass)#15 (3) {
          ["categorie"]=>
          string(7) "Bonbons"
          ["marque"]=>
          string(6) "MENTOS"
          ["nom"]=>
          string(31) "Bonbons framboise orange MENTOS"
        }
      }
      [6]=>
      object(stdClass)#16 (2) {
        ["_id"]=>
        string(20) "lt5iEXIBfVAlCluoT3sT"
        ["_source"]=>
        object(stdClass)#17 (3) {
          ["categorie"]=>
          string(7) "Bonbons"
          ["marque"]=>
          string(6) "MENTOS"
          ["nom"]=>
          string(26) "Bonbons pomme verte MENTOS"
        }
      }
    }
  }
}

【问题讨论】:

  • 你想从 JSON 中准确提取什么数据?

标签: javascript php jquery json elasticsearch


【解决方案1】:

你可以使用

1. Response filtering

filter_path 参数,可用于减少返回的响应 由弹性搜索。此参数采用逗号分隔的列表 用点符号表示的过滤器:

例如。 filter_path=hits.hits._source

2. Source filtering

_source:可用于指定要返回的字段。默认返回所有字段

GET <index_name>/_search?filter_path=hits.hits._source
{
  "query": {
    "match_all": {}
  },
  "_source": ["categorie","marque","nom"]
}

结果

  "hits" : {
    "hits" : [
      {
        "_source" : {
          "categorie" : "Bonbons",
          "nom" : "Bonbons pomme verte MENTOS",
          "marque" : "MENTOS"
        }
      }
    ]
  }
}

【讨论】:

  • 谢谢。当我使用 php 将其转换为数组后解析此数据时,您的回答现在很有帮助,但结果为空
  • @Ana 你能添加你的 php 查询吗
  • @Ana 我不知道 php sytnax。从我收集的文档中,它应该类似于以下 $params = [ 'index' => 'my_index','filter_path':"hits.hits._source" 'body' => [ 'query' => [ 'match' = > [ 'testField' => 'abc' ] ] ] ]; $results = $client->search($params); $doc = $results['hits']['hits'][0]['_source'];
  • @Ana Glad 可能会有所帮助
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2013-11-14
  • 1970-01-01
  • 2022-10-24
相关资源
最近更新 更多