我在 C# 中创建了一个 Web 服务,如下所示:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string UpdateHeatCallJSON(string json)
{
HeatItem item = JsonConvert.DeserializeObject<HeatItem>(json);
UpdateHeatCall(item);
HeatItemResponse response = new HeatItemResponse();
//... more code
return JsonConvert.SerializeObject(response);
}
我基本上有一个对象 HeatItem,我想将其作为参数传入。
我目前在 C# 控制台应用程序中使用 Web 服务(用于测试)并且在寻找正确格式时遇到了挑战。这是我的电话:
static void UpdateHeatItemJSON()
{
// corrected to WebRequest from HttpWebRequest
WebRequest request = WebRequest.Create(requestServer + "/UpdateHeatCallJSON");
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
string postData = "";
postData = @"{'json':'{""BusinessPartner"":""00000000-0000-0000-0000-000000000000"",""CaseNumber"":4,""CaseDescription"":""first case"",""CaseType"":"""",""CaseSeverity"":"""",""DueDate"":""0001-01-01T00:00:00"",""AssignmentNumber"":5,""AssignmentDescription"":"""",""AssignmentCreation"":""0001-01-01T00:00:00"",""AssignmentTime"":""0001-01-01T00:00:00"",""ChangeDate"":""0001-01-01T00:00:00"",""ChangeTime"":""0001-01-01T00:00:00"",""Group"":"""",""SubGroup"":"""",""Module"":"""",""AssignmentStatus"":"""",""KPIChallenge"":false,""KPI1Status"":"""",""KPI1User"":"""",""KPI1Date"":""0001-01-01T00:00:00"",""KPI1Time"":""0001-01-01T00:00:00"",""KPI2Status"":"""",""KPI2User"":"""",""KPI2Date"":""0001-01-01T00:00:00"",""KPI2Time"":""0001-01-01T00:00:00"",""SessionID"":null}'}";
System.Diagnostics.Debug.Print(postData);
//get a reference to the request-stream, and write the postData to it
using (Stream s = request.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(postData);
}
//get response-stream, and use a streamReader to read the content
using (Stream s = request.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
string jsonData = sr.ReadToEnd();
JObject jObject = JObject.Parse(jsonData);
JToken jHeatItem = jObject["HeatItem"];
}
}
}
看看上面的 postData,格式化 json 字符串是可行的,但是当我在http://jsonlint.com/验证字符串时,验证失败。当我像这样传入经过验证的 json 字符串时:
{“json”:{“BusinessPartner”:“00000000-0000-0000-0000-000000000000”,“CaseNumber”:4,“CaseDescription”:“第一个案例”,“CaseType”:“”,“CaseSeverity”:“” ,“DueDate”:“0001-01-01T00:00:00”,“AssignmentNumber”:5,“AssignmentDescription”:“”,“AssignmentCreation”:“0001-01-01T00:00:00”,“AssignmentTime”: “0001-01-01T00:00:00”、“更改日期”:“0001-01-01T00:00:00”、“更改时间”:“0001-01-01T00:00:00”、“组”:“” , "子组": "", "模块": "",“AssignmentStatus”:“”,“KPIChallenge”:假,“KPI1Status”:“”,“KPI1User”:“”,“KPI1Date”:“0001-01-01T00:00:00”,“KPI1Time”:“0001- 01-01T00:00:00”、“KPI2Status”:“”、“KPI2User”:“”、“KPI2Date”:“0001-01-01T00:00:00”、“KPI2Time”:“0001-01-01T00: 00:00", "SessionID": null } }0001-01-01T00:00:00”、“KPI2Status”:“”、“KPI2User”:“”、“KPI2Date”:“0001-01-01T00:00:00”、“KPI2Time”:“0001-01- 01T00:00:00", "SessionID": null } }0001-01-01T00:00:00”、“KPI2Status”:“”、“KPI2User”:“”、“KPI2Date”:“0001-01-01T00:00:00”、“KPI2Time”:“0001-01- 01T00:00:00", "SessionID": null } }
Web 服务返回 500 内部服务器错误,我认为这是因为它不喜欢传入的参数。我对此比较陌生,我想知道是否有人可以帮助我更好地理解这一点。
还查看响应(对于工作示例),返回 json 包含三个反斜杠 \\ 我相信可以转义引号。这使得这个电话
JObject jObject = JObject.Parse(jsonData);
JToken jHeatItem = jObject["HeatItem"];
失败。任何想法为什么?
谢谢托马斯