按特定值获取JSON数据

时间:2017-06-02 14:53:56

标签: php mysql json

我正在努力完成这项工作,我希望有人可以帮助我朝着正确的方向努力。我正在创建一个从多个来源获取数据的信息中心:Google Analytics,Google趋势等。由于这是一个概念证明,我试图通过使用具有以下结构的JSON文件来模拟不同的源:

{
  "data":
  [
    {
      "source":"google analytics",
      "data":
      [
        {
          "month":"jan",
          "visitors":"593"
        },
        {
          ...
        }
      ]
    },
    {
      "source":"google trends",
      "keywords":
      [
        {
          "value":"keyword 1",
          "data":
          [
            {
              "month":"dec",
              "popularity":"10"
            },
            {
              ...
            }
          ]
        },
        {
          "value":"keyword 2",
          "data": ...
        }
      ]
    }
  ]
}

接下来,我有一个下拉列表,其中包含来自MySQL数据库查询的关键字。当用户从该列表中选择关键字时,我希望显示与该特定关键字匹配的JSON文件中的数据。

例如,当用户从下拉列表中选择“关键字1”时,需要获取值“Dec”和“100”,然后Chart.js将使用这些值

现在我有以下代码:

$dataFile = json_decode(file_get_contents($src));
$data = $dataFile->data[1]->keywords[0]; // Google Trends

foreach ($data->results as $result) {
  echo "<li><p>" . $result->month . "</p></li>";
  echo "<li><p>" . $result->popularity . "</p></li>";
}

这确实显示了正确的数据,即月份和流行度分数,但显然仅适用于使用数组值定义的关键字。如何为所选关键字(通过GET发布)制作此节目数据?

我要做的是:

$dataFile = json_decode(file_get_contents($src));
$data = $dataFile->data["google trends"]->keywords[$selectedKeyword];

foreach ($data->results as $result) {
  echo "<li><p>" . $result->month . "</p></li>";
  echo "<li><p>" . $result->popularity . "</p></li>";
}

我知道json_decode的第二个参数,它使它成为一个关联数组,但我不知道如何使用它来完成我想要做的事情。

目前这是$ data的结果:

object(stdClass)#10 (2) {
  ["value"]=>
  string(10) "keyword 1"
  ["data"]=>
  array(7) {
    [0]=>
    object(stdClass)#11 (2) {
      ["month"]=>
      string(3) "dec"
      ["popularity"]=>
      string(2) "10"
    }
    [1]=>
    object(stdClass)#12 (2) {
      ["month"]=>
      string(3) "jan"
      ["popularity"]=>
      string(1) "5"
    }
    [2]=>
    object(stdClass)#13 (2) {
      ["month"]=>
      string(3) "feb"
      ["popularity"]=>
      string(1) "8"
    }
    [3]=>
    object(stdClass)#14 (2) {
      ["month"]=>
      string(3) "mar"
      ["popularity"]=>
      string(1) "5"
    }
    [4]=>
    object(stdClass)#15 (2) {
      ["month"]=>
      string(3) "apr"
      ["popularity"]=>
      string(2) "10"
    }
    [5]=>
    object(stdClass)#16 (2) {
      ["month"]=>
      string(3) "mei"
      ["popularity"]=>
      string(2) "20"
    }
    [6]=>
    object(stdClass)#17 (2) {
      ["month"]=>
      string(3) "jun"
      ["popularity"]=>
      string(2) "10"
    }
  }
}

2 个答案:

答案 0 :(得分:1)

您的初始示例似乎忽略了所有关键字结果都是在数据对象键下进行了分组。您在JSON对象中有一些集合需要迭代才能访问下一级别。下面的示例是一种动态挖掘数据对象的方法

$dataFile = json_decode(file_get_contents($src));

$source = getSource($dataFile->data, $dataSource);

foreach ($source->keywords as $group) {
  if ($group->value === $selectedKeyword) {
    buildList($group);
  }
}

function buildList($group)
{
  foreach ($group->data as $listItem) {
    echo "<li><p>" . $listItem->month . "</p></li>";
    echo "<li><p>" . $listItem->popularity . "</p></li>";
  }
}


function getSource($dataCollection, $key)
{
  foreach ($dataCollection as $entry) {
    if ($entry->source === $key) {
      return $entry;
    }
  } 
}

答案 1 :(得分:1)

您无法使用给定的输入json文件以这种方式访问​​对象,您可以执行以下操作

$selected_keyword = 'keyword 1';

$dataFile = json_decode(file_get_contents($src));


foreach($dataFile->data as $trend)
{
    /* Source you are looking */
    if($trend->source == 'google trends')
    {
        foreach($trend->keywords as $keyword)
        {
            /* Keyword which I am looking for */
            if($keyword->value == $selected_keyword)
            {
                foreach ($keyword->data as $result) 
                {
                    echo "<li><p>" . $result->month . "</p></li>";
                    echo "<li><p>" . $result->popularity . "</p></li>";
                }
            }
        }
    }

}

以下是测试结果

输入 - t.json

akshay@db-3325:/tmp$ cat t.json 
{
  "data":
  [
    {
      "source":"google analytics",
      "data":
      [
        {
          "month":"jan",
          "visitors":"593"
        }

      ]
    },
    {
      "source":"google trends",
      "keywords":
      [
        {
          "value":"keyword 1",
          "data":
          [
            {
              "month":"dec",
              "popularity":"100"
            }
          ]
        },
        {
          "value":"keyword 2",
          "data": []
        }
      ]
    }
  ]
}

脚本 - t.php

akshay@db-3325:/tmp$ cat t.php 
<?php

$selected_keyword = 'keyword 1';

$dataFile = json_decode(file_get_contents('t.json'));

/* Your input */
print_r($dataFile);

foreach($dataFile->data as $trend)
{
    /* Source you are looking */
    if($trend->source == 'google trends')
    {
        foreach($trend->keywords as $keyword)
        {
            if($keyword->value == $selected_keyword)
            {
                foreach ($keyword->data as $result) 
                {
                    /* Your output */
                    echo "<li><p>" . $result->month . "</p></li>";
                    echo "<li><p>" . $result->popularity . "</p></li>";
                }
            }
        }
    }

}
?>

执行&amp;输出

akshay@db-3325:/tmp$ php t.php 
stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [source] => google analytics
                    [data] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [month] => jan
                                    [visitors] => 593
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [source] => google trends
                    [keywords] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [value] => keyword 1
                                    [data] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [month] => dec
                                                    [popularity] => 100
                                                )

                                        )

                                )

                            [1] => stdClass Object
                                (
                                    [value] => keyword 2
                                    [data] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)
<li><p>dec</p></li><li><p>100</p></li>