如何在php中读取json文件和打印

时间:2015-01-31 15:51:50

标签: php json

以下.json文件中的数据。我想阅读并打印数据。

{" employees" :[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName": "Jones"}
]};

1 个答案:

答案 0 :(得分:2)

您的json文件 ("/something/abc.json")

{"employees":[
    {
        "firstName":"Ray",
        "lastName":"Villalobos",
        "joined":2012
    },
    {
        "firstName":"John",
        "lastName":"Jones",
        "joined":2010
    }
]}


您的PHP代码,

<?php
$string = file_get_contents("/something/abc.json");
$arr = json_decode($string,true);

foreach($arr as $a)
{
    // Iteration
}
?>