1

Objective: To parse following json string and get mentioned values separately, later those separated values are going to be inserted to mysql database.
I have checked my json string on JsonLint

  1. user_name,
  2. selected_date,
  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 code is:

    $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";
     }
    }
    

As i am new to JSON and PHP, i could not solve this, help would be appreciated.

0

1 Answer 1

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>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much @PHPhil, better one in such a small time span.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.