Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON从C#发送,但在php中未作为JSON接收_C#_Php_Json - Fatal编程技术网

JSON从C#发送,但在php中未作为JSON接收

JSON从C#发送,但在php中未作为JSON接收,c#,php,json,C#,Php,Json,我的目的是将JSON从C#发送到PHP,然后在PHP端对其进行进一步解码以用于我的目的。 PHP端的错误是: 警告:json_decode()要求参数1为字符串,数组在第24行的/home3/alsonsrnd/public_html/test/sync.php中给出 我使用以下代码使用C#将JSON发送到我的PHP代码: 在PHP上,我使用带斜杠和json_decode()来解码json。我的PHP代码给出了一个错误,即接收的字符串是数组而不是JSON,因此无法使用JSON_decode对其进

我的目的是将JSON从C#发送到PHP,然后在PHP端对其进行进一步解码以用于我的目的。
PHP端的错误是:

警告:json_decode()要求参数1为字符串,数组在第24行的/home3/alsonsrnd/public_html/test/sync.php中给出

我使用以下代码使用C#将JSON发送到我的PHP代码:

在PHP上,我使用带斜杠和json_decode()来解码json。我的PHP代码给出了一个错误,即接收的字符串是数组而不是JSON,因此无法使用JSON_decode对其进行解码。原因可能是什么?因为我正在从C发送正确格式的JSON#

我的PHP代码是:

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $json=$_POST;
    if (get_magic_quotes_gpc()){
        $json = stripslashes($json);
    }

    //Decode JSON into an Array
    $data = json_decode($json);
}

首先,您必须更改内容类型标题

httpWebRequest.ContentType = "application/x-www-form-urlencoded";
其次,您正在以错误的格式发送数据

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "json=" + "{\"user\":\"test\"," +
                              "\"password\":\"bla\"}"; ;

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }
最后,您以错误的方式接收数据,$\u POST是一个数组,所以
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['json']))
{
    $json=$_POST['json'];
    if (get_magic_quotes_gpc()){
        $json = stripslashes($json);
    }
    //Decode JSON into an Array
    $data = json_decode($json, true);
}

它存在于代码中。字符串json=…是打字错误吗?变量名称似乎不匹配。能否在PHP服务器上添加调试行以打印它接收的JSON?(斜杠剥离前后)我们能看到PHP代码吗?请看这里的第一个答案-
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['json']))
{
    $json=$_POST['json'];
    if (get_magic_quotes_gpc()){
        $json = stripslashes($json);
    }
    //Decode JSON into an Array
    $data = json_decode($json, true);
}