我已經建立在C#中的web服務,其看起來像這樣:.NET框架2.0 C#和JSON webquery格式字符串
[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」:{ 「業務夥伴」: 「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」, 「ChangeDate」:「0001-01-01T00: 00:00「, 」ChangeTime「:」0001-0 1-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 } }
t他web服務返回500內部服務器錯誤,我相信是因爲它不喜歡傳入的參數。我相對較新,我想知道是否有人可以幫助我更好地理解這一點。
另外看看響應(對於工作示例),返回json包含三個backslaches \\我相信要避免引號。這使得這個電話
JObject jObject = JObject.Parse(jsonData);
JToken jHeatItem = jObject["HeatItem"];
失敗。任何想法爲什麼?
感謝 托馬斯
的Json字符串需要用雙引號,而不是單引號。 –