使用 PHP 脚本读取 json 文件并打印输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19147662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:54:38  来源:igfitidea点击:

Read a json file and print output using a PHP script

phpjson

提问by rond

I have to write a script to read a JSON file which will have information about a Data Pipeline - mainly three components - Status, Comments and Timestamp. Once I read the file in JSON, I need to print out the output for that respective Data Pipeline with all the three components. The output would look something like this as below:

我必须编写一个脚本来读取一个 JSON 文件,该文件将包含有关数据管道的信息 - 主要是三个组件 - 状态、注释和时间戳。以 JSON 格式读取文件后,我需要打印出相应数据管道的所有三个组件的输出。输出如下所示:

Name: Apollo Status: Pending Comments: Monthly report Timestamp: 00:00

名称:Apollo 状态:待定意见:月报时间戳:00:00

I have the following script to generate the JSON file:

我有以下脚本来生成 JSON 文件:

<?php
    $data = array(
             "name"=>"Apollo", 
             "cob"=> array(
                          status=> "completed",
                          comment=> "Monthly report",
                          timestamp=> "00:00"
                         ),
              );

header('Content-Type: application/json');
echo json_encode($data);
?>

I have the following script to read the JSON file generated and to print the above required output:

我有以下脚本来读取生成的 JSON 文件并打印上述所需的输出:

<?php
    $data = file_get_contents ('./cob_details.json');
    $json = json_decode($data, true);

    echo ('<pre>');
    print_r ($json);
    echo ('</pre>');

    echo ('<br>output:</br>');

    foreach ($json as $key => $value)
    {
    echo "Name: $value Status: $value]<br />";
    }
?>

I am particularly new to JSON and PHP....Can you please let me know what am I missing over here in this script to get the required output or where am I going wrong?

我对 JSON 和 PHP 特别陌生....你能告诉我我在这个脚本中遗漏了什么以获得所需的输出或者我哪里出错了吗?

回答by RiggsFolly

I think the problem is that your json data generator has a minor error

我认为问题是你的json数据生成器有一个小错误

Try this instead, field names in an array have to be text literals and you forgot to wrap the names in quotes

试试这个,数组中的字段名称必须是文本文字,而您忘记将名称用引号括起来

<?php
    $data = array(
             "name" => "Apollo",
             "cob"  => array(
                          'status'    => "completed",
                          'comment'   => "Monthly report",
                          'timestamp' => "00:00"
                         )
              );

    echo json_encode($data);
?>

Now the code that read the data needs to loop over the first array and then grab the bits it is interested in from the inner array using the $valuevariable which is the address of the inner array. I am assuming the actual data contains more fields than your sample code so I used a switchbut an ifwould do if its not that complicated

现在读取数据的代码需要遍历第一个数组,然后使用作为内部数组$value地址的变量从内部数组中获取它感兴趣的位。我假设实际数据包含比您的示例代码更多的字段,所以我使用了一个switchif如果它不那么复杂就会做

<?php
    $data = file_get_contents ('./cob_details.json');
    $json = json_decode($data, TRUE);

    echo ('<pre> print the json ');
    print_r ($json);
    echo ('</pre>');

    echo '<br>output:</br>';

    foreach ($json as $key => $value)
    {
        switch ( $key ) {
            case 'name' :
                echo "Name: $value";
                break;
            case 'cob' : 
                echo ' Status: ' . $value['status'] . ']<br />';
                break;
            case 'another field' :
                // and so on
                break;
        }

}

回答by Indrajeet Singh

Try:

尝试:

$data = file_get_contents ('./cob_details.json');
$json = json_decode($data, true);
foreach ($json as $key => $value) {
    if (!is_array($value)) {
        echo $key . '=>' . $value . '<br/>';
    } else {
        foreach ($value as $key => $val) {
            echo $key . '=>' . $val . '<br/>';
        }
    }
}