在PHP页面上从JSON获取数据时出现问题

时间:2011-08-10 06:53:00

标签: php json

我发送请求

$.ajax({
    url: 'php_scripts/reload_fancoils.php',
    type: 'POST',
    dataType: 'json',
    data:parameters,
    success: function(data){
        //alert(data);
        console.log(data);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert('statusText='+xhr.statusText+' responseText='+xhr.responseText);
    }   

});

参数

{
    "command": "write_plan_to_db",
    "fan_coil_id": 44444,
    "plan": {
        "points": {
            "mon": [
                "{\"start_time\":0, \"end_time\":840,\"mode\":0}",
                "{\"start_time\":840, \"end_time\":1440,\"mode\":2}"
            ],
            "tue": [
                "{\"start_time\":0, \"end_time\":1440,\"mode\":0}"
            ],
            "wed": [
                "{\"start_time\":0, \"end_time\":1440,\"mode\":1}"
            ],
            "thu": [
                "{\"start_time\":0, \"end_time\":1440,\"mode\":2}"
            ],
            "fri": [
                "{\"start_time\":0, \"end_time\":1440,\"mode\":4}"
            ],
            "sat": [
                "{\"start_time\":0, \"end_time\":1440,\"mode\":0}"
            ],
            "sun": [
                "{\"start_time\":0, \"end_time\":1440,\"mode\":1}"
            ]
        },
        "modes": {
            "mode_1": {
                "temperature_set_point": 18,
                "max_positive_variation": 0,
                "min_positive_variation": 0,
                "max_fan_speed": 4
            },
            "mode_2": {
                "temperature_set_point": 18,
                "max_positive_variation": 0,
                "min_positive_variation": 0,
                "max_fan_speed": 4
            },
            "mode_3": {
                "temperature_set_point": 18,
                "max_positive_variation": 0,
                "min_positive_variation": 0,
                "max_fan_speed": 4
            },
            "mode_4": {
                "temperature_set_point": 18,
                "max_positive_variation": 0,
                "min_positive_variation": 0,
                "max_fan_speed": 4
            },
            "mode_5": {
                "temperature_set_point": 18,
                "max_positive_variation": 0,
                "min_positive_variation": 0,
                "max_fan_speed": 4
            }
        }
    }
}

如何在服务器端,php页面读取'mode_1'的值?

2 个答案:

答案 0 :(得分:2)

使用json_decode函数,例如

$var = json_decode($_POST['parameters']);

var_dump($var);

答案 1 :(得分:1)

$plan = json_decode($_POST['plan']);
$mode1 = $plan->modes->mode_1;