编辑:我设法通过将WebMethod设置为void而不是字符串然后手动写入响应来实现它的工作:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
HttpContext.Current.Response.Write(json);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
我有一个webmethod,它返回一个序列化的json对象,下面是一个例子:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string PostDropDowns()
{
TestClass t = new TestClass()
{
abc = "this has been sent over",
Strings = new List<string> { "fdfdfd", "fdfdfdf" }
};
var json = JsonConvert.SerializeObject(t);
return json;
}
JSON text =
{"abc":"this has been sent over","Strings":["fdfdfd","fdfdfdf"]}
在我的c#UWP应用程序中,我称之为webrequest
public static void GetDropDowns(string address)
{
//create the HTTPWebRequest
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContinueTimeout = 20000;
// request.Accept = "text/json";
request.ContentType = "application/json; charset=utf-8";
try
{
//get the response
using (var response = request.GetResponseAsync().Result)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
var responseFromServer = reader.ReadToEnd();
var dds = JsonConvert.DeserializeObject<TestClass>(responseFromServer);
}
}
catch (Exception e)
{
//error handling in here
}
}
然而,我回来的json看起来像这样:
{"d":"{\"abc\":\"this has been sent over\",\"Strings\":[\"fdfdfd\",\"fdfdfdf\"]}"}
因此,当反序列化回我的TestClass时,标题和字符串都为空。
我如何预防d通过或绕过它?
提前致谢