Parse Json data in php

Hi,
I have a json output like below, How can i parse this values in a php file ? Thanks in advance

{"participants":[{"NoPrticiField":1,"empname":"gfh","designation":"fh","email":"fgh"},{"NoPrticiField":2,"empname":"fgh","designation":"fgh","email":"fgh"}]}:

$json = json_decode($output); 

If I remember correctly…

Hi Patche,

I tried this, But it’s not working, Below is the jquery.ajax i am using

        $.ajax({
            type: 'POST',
            url: 'data11.php',
            data: { json: ko.toJSON(viewModel) },
            
                 
            success: function (result) {
                alert(result);
                
            }

        });

Can you paste any error messages or output from the page to here?

In my below code, echo is giving This Error “Catchable fatal error: Object of class stdClass could not be converted to string”

$value = json_decode($_POST[‘json’]);
echo($value);

Using the JSON from your first post as an example, calling json_decode will give you back an object, which you can access like this:


echo $value->participants[0]->empname;
// Outputs: gfh

If you want to check the contents of $value, you can call var_dump($value);

Thanks fretburner, Patche… It works … :slight_smile:

I have 2 variables, var selectPartiNo = ‘2’; and var event_id = “4”; , I am currently passing it’s value into my php file like url: ‘data11.php?event_id=’ + event_id + ‘&selectPartiNo=’+selectPartiNo+‘’, … Is there any other method to pass these variable values ???


        processClick: function () {
            var selectPartiNo = '2';
            var event_id = "4";
            ko.applyBindings(viewModel, $("#registrationWrap")[0]);

            $.ajax({
                type: 'POST',
                url: 'data11.php?event_id=' + event_id + '&selectPartiNo='+selectPartiNo+'',
                data: { json: ko.toJSON(viewModel) },
                eventID:"+event_id+",
                     
                success: function (result) {
                    alert(result);
                    
                }

            });

        }

In data11.php code is like below


echo "Event id   : ".$_GET['event_id']."<br>";
echo "Selecte Number  : ".$_GET['selectPartiNo']."<br>";

I’m not sure if this is what you’re asking, but you could pass the variables by POST, along with the json string:

$.ajax({
    type: 'POST',
    url: 'data11.php',
    data: {
    	json: ko.toJSON(viewModel),
    	event_id: event_id,
    	selectPartiNo: selectPartiNo
	},
    success: function (result) {
        alert(result);
    }
});

echo "Event id   : " . $_POST['event_id'] . "<br>";
echo "Selected Number  : " . $_POST['selectPartiNo'] . "<br>";

Note that it would be a good idea to sanitize any POST/GET variables before using them in your script.