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
- user_name,
- selected_date,
- selected_project,
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.