1
我想通过JSON.NET与JSON.NET在C#.NET应用程序和某个PHP服务器端之间发生一些通信。我在发布Json字符串,但是我无法访问(或没有正确发送)服务器端的字符串。我的$ _POST变量显示为空,我不知道用什么键来访问Json字符串。任何人都可以提出建议Json.NET和PHP通信
我的C#代码:
TestClass ts = new TestClass("Some Data", 3, 4.5);
string json = JsonConvert.SerializeObject(ts);
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://localhost/testJson.php");
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = json.Length;
StreamWriter sw = new StreamWriter(request.GetRequestStream());
sw.Write(json);
sw.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string responseJson = sr.ReadToEnd();
sr.Close();
TestClass returnedTS =
JsonConvert.DeserializeObject<TestClass>(responseJson);
我的PHP代码:
<?php
require("TestClass.php");
$json = json_decode($_POST);
$ts = new TestClass();
$ts->someString = $json['someString'];
$ts->someDouble = $json['someDouble'];
$ts->someInt = $json['someInt'];
$return_json = json_encode($ts);
echo $return_json;
?>
我的输出:
<b>Warning</b>: json_decode() expects parameter 1 to be string, array given in
<b>C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\testJson.p
hp</b> on line <b>4</b><br />
{"someString":null,"someInt":null,"someDouble":null}