【问题标题】:Parse JSON on PHP and extract the particular value(s)在 PHP 上解析 JSON 并提取特定值
【发布时间】:2015-06-15 16:07:21
【问题描述】:

目标:解析后面的 json 字符串并单独获取提到的值,稍后将这些分离的值插入到 mysql 数据库。
我在JsonLint 上检查了我的json 字符串

  1. 用户名,
  2. 选定日期,
  3. selected_project,
  4. tasks , 4.1.task_name, 4.2 work_hours

    {
    "user_name": "USER",
    "selected_date": "2015-06-08",
    "selected_project": "Project1",
    "tasks": [
        {
            "task_name": "task-1",
            "work_hours": [
                {
                "Monday": " 3"
                },
                {
                "Tuesday": " 0"
                },
                {
                "Wednesday": " 2.5"
                },
                {
                "Thursday": " 2"
                },
                {
                "Friday": " 0"
                },
                {
                "Saturday": " 0"
                },
                {
                "Sunday": " 0"
                }
            ]
        }
    ] 
    }
    

    PHP代码是:

    $str_json = file_get_contents('php://input'); //($_POST doesn't work here)
    $response = json_decode($str_json, true); // decoding received JSON to array
    $jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($response, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);
    foreach ($jsonIterator as $key => $val) 
    {
     if(is_array($val)) 
     {
       echo "$key:\n";
     } 
     else 
     {
       echo "$key => $val\n";
       echo "$value";
     }
    }
    

由于我是 JSON 和 PHP 的新手,我无法解决这个问题,我们将不胜感激。

【问题讨论】:

    标签: php mysql json parsing logic


    【解决方案1】:
        <?
    $json = '{
    "user_name": "USER",
    "selected_date": "2015-06-08",
    "selected_project": "Project1",
    "tasks": [
        {
            "task_name": "task-1",
            "work_hours": [
                {
                "Monday": " 3"
                },
                {
                "Tuesday": " 0"
                },
                {
                "Wednesday": " 2.5"
                },
                {
                "Thursday": " 2"
                },
                {
                "Friday": " 0"
                },
                {
                "Saturday": " 0"
                },
                {
                "Sunday": " 0"
                }
            ]
        }
    ] 
    }';
    
    // decode your json  into associative arrays
    $decoded = json_decode($json, true);
    
    // use array
    echo "Username: ". $decoded['user_name'] . "<br>";
    echo "Date: ". $decoded['selected_date'] . "<br>";
    echo "project: ". $decoded['selected_project'] . "<br>";
    echo "Task: ". $decoded['tasks'][0]['task_name'] . "<br>";
    
    foreach($decoded['tasks'][0]['work_hours'] as $key => $value) {
        foreach($value as $key2 => $value2){
            echo $key2 . ": ". $value2 . "<br>";
        }
    }
    ?>
    

    【讨论】:

    • 非常感谢@PHPhil,在这么短的时间内做得更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多