Note that there are some explanatory texts on larger screens.

plurals
  1. PO.net framework 2.0 C# and json webquery format string
    text
    copied!<p>I have created a webservice in C# which looks like this:</p> <pre><code> [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string UpdateHeatCallJSON(string json) { HeatItem item = JsonConvert.DeserializeObject&lt;HeatItem&gt;(json); UpdateHeatCall(item); HeatItemResponse response = new HeatItemResponse(); //... more code return JsonConvert.SerializeObject(response); } </code></pre> <p>I basically have an object HeatItem, which I want to pass in as an argument.</p> <p>I currently consume the web service (for testing) in a C# console application and having challenges finding the correct format. This is my call:</p> <pre><code> 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"]; } } } </code></pre> <p>Looking at postData above, formatting the json string as above works, however when I validate the string at <a href="http://jsonlint.com/" rel="nofollow">http://jsonlint.com/</a> the validation fails. When I pass in a validated json string like this:</p> <p>{ "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 } }</p> <p>the webservice returns a 500 Internal Server Error, which I believe is because it does not like the argument passed in. I am relatively new to this and I am wondering if anyone can help me understand this a bit better.</p> <p>Also looking at the response (for the working example), the return json contains three backslaches \\ I believe to escape the quotation marks. This makes this call</p> <pre><code> JObject jObject = JObject.Parse(jsonData); JToken jHeatItem = jObject["HeatItem"]; </code></pre> <p>to fail. Any ideas why?</p> <p>Thanks Thomas </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload