【问题标题】:how to get particular node value fron json bing response in php如何从 php 中的 json bing 响应中获取特定的节点值
【发布时间】:2018-08-07 09:44:06
【问题描述】:

我正在尝试使用 bing 搜索 API,我得到了 JSON 格式的结果,现在我想从该 JSON 结果中获取特定的节点值,例如(url、displayurl、sn-p 等)。

这是我得到的 JSON 结果格式

JSON 响应:

{
  "_type": "SearchResponse",
  "queryContext": {
    "originalQuery": "infokart India Pvt. Ltd."
  },
  "webPages": {
    "webSearchUrl": "https://www.bing.com/search?q=infokart+India+Pvt.+Ltd.",
    "totalEstimatedMatches": 12200000,
    "value": [
      {
        "id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.0",
        "name": "Infokart India Pvt. Ltd.",
        "url": "http://infokartindia.com/",
        "isFamilyFriendly": true,
        "displayUrl": "infokartindia.com",
        "snippet": "Journals Subscription Services,International Subscription Agency,Experiences subscription agency",
        "dateLastCrawled": "2018-07-24T11:49:00.0000000Z",
        "language": "en"
      }
    ]
  },
  "rankingResponse": {
    "mainline": {
      "items": [
        {
          "answerType": "WebPages",
          "resultIndex": 0,
          "value": {
            "id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.0"
          }
        }
      ]
    }
  }
}

请推荐!!

【问题讨论】:

  • 不确定编辑是否更好。 OP 格式化您的 json,以便我们可以读取它。
  • 你想要的格式@aandreas
  • 请使用 json_decode 将 Json 转换为数组。之后,您可以从数组中检索节点值。
  • @Harinarayan 对其进行格式化以使其可读。看看 var_dump 的样子3v4l.org/dAbYa

标签: php json bing bing-api


【解决方案1】:
<?php 

$json = '{"_type":"SearchResponse","queryContext":{"originalQuery":"infokart India Pvt. Ltd."},"webPages":{"webSearchUrl":"https://www.bing.com/search?q=infokart+India+Pvt.+Ltd.","totalEstimatedMatches":12200000,"value":[{"id":"https://api.cognitive.microsoft.com/api/v7/#WebPages.0","name":"Infokart India Pvt. Ltd.","url":"http://infokartindia.com/","isFamilyFriendly":true,"displayUrl":"infokartindia.com","snippet":"Journals Subscription Services,International Subscription Agency,Experiences subscription agency","dateLastCrawled":"2018-07-24T11:49:00.0000000Z","language":"en"}]},"rankingResponse":{"mainline":{"items":[{"answerType":"WebPages","resultIndex":0,"value":{"id":"https://api.cognitive.microsoft.com/api/v7/#WebPages.0"}}]}}}';

$json_data = json_decode($json,true);

foreach($json_data['webPages']['value'] as $each_data){
    foreach($each_data as $each_key => $each_value){
        if(in_array(strtolower($each_key),['url','displayurl','snippet'])){
            echo $each_key," => ",$each_value,"<br/>";
        }
    }
}

输出

url => http://infokartindia.com/
displayUrl => infokartindia.com
snippet => Journals Subscription Services,International Subscription Agency,Experiences subscription agency

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    相关资源
    最近更新 更多