如何从文件内容中获取JSON中的标签数据

时间:2016-11-27 07:10:07

标签: php json

$json = file_get_contents('module=API&method=Referrers.getReferrerType&format=json&period=day&date=yesterday&disableLink=1&idSite=3');
$data = json_decode($json,true);

json数据:

[{"label":"Direct Entry","nb_uniq_visitors":526,"nb_visits":593,"nb_actions":768,"nb_users":0,"max_actions":32,"sum_visit_length":83153,"bounce_count":513,"nb_visits_converted":0,"segment":"referrerType==direct"},{"label":"Search Engines","nb_uniq_visitors":230,"nb_visits":235,"nb_actions":631,"nb_users":0,"max_actions":71,"sum_visit_length":52233,"bounce_count":150,"nb_visits_converted":0,"segment":"referrerType==search","idsubdatatable":2},{"label":"Websites","nb_uniq_visitors":7,"nb_visits":7,"nb_actions":20,"nb_users":0,"max_actions":5,"sum_visit_length":835,"bounce_count":1,"nb_visits_converted":0,"segment":"referrerType==website","idsubdatatable":3}]

我正在尝试显示引荐来源类型的每个标签。

  1. 直接输入

  2. 搜索引擎

  3. 网站

  4. $data[0]->label;不起作用

    $data->label;不起作用

    $data->label[0];不起作用

2 个答案:

答案 0 :(得分:0)

您可以通过将true传递到json_decode方法的第二个参数,将json数据转换为关联数组(嵌套)来尝试此操作:

$json = "[{\"label\":\"Direct Entry\",\"nb_uniq_visitors\":526,\"nb_visits\":593,\"nb_actions\":768,\"nb_users\":0,\"max_actions\":32,\"sum_visit_length\":83153,\"bounce_count\":513,\"nb_visits_converted\":0,\"segment\":\"referrerType==direct\"},{\"label\":\"Search Engines\",\"nb_uniq_visitors\":230,\"nb_visits\":235,\"nb_actions\":631,\"nb_users\":0,\"max_actions\":71,\"sum_visit_length\":52233,\"bounce_count\":150,\"nb_visits_converted\":0,\"segment\":\"referrerType==search\",\"idsubdatatable\":2},{\"label\":\"Websites\",\"nb_uniq_visitors\":7,\"nb_visits\":7,\"nb_actions\":20,\"nb_users\":0,\"max_actions\":5,\"sum_visit_length\":835,\"bounce_count\":1,\"nb_visits_converted\":0,\"segment\":\"referrerType==website\",\"idsubdatatable\":3}]";

$data = json_decode($json, true);
print_f($data[0]['label']);

希望这有帮助!

答案 1 :(得分:0)

您已将true传递给json_decode()的第二个参数。这使它返回一个关联数组。因此,您可以简单地使用这样的数据:

$data[0]["label"]