0

Edit: I've managed to get it working by setting the WebMethod to a void rather than a string and then writing to the response manually:

        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();

I have a webmethod which returns a serialised json object, below is an example:

[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;
    }

The JSON text =

{"abc":"this has been sent over","Strings":["fdfdfd","fdfdfdf"]}

In my c# UWP application I am calling this 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

        }
    }

However, the json I am getting back looks like this:

{"d":"{\"abc\":\"this has been sent over\",\"Strings\":[\"fdfdfd\",\"fdfdfdf\"]}"}

So when deserialize back to my TestClass both Title and Strings are null.

How do I preve that d coming through or get round it?

Thanks in advance

iandavidson1982
  • 190
  • 3
  • 17
  • As far as I know that is by design. Can't you just work with it as is? – Crowcoder Jan 22 '18 at 16:01
  • I suppose that's part of the question. How? It looks like it thinks it's of type d: – iandavidson1982 Jan 22 '18 at 16:05
  • I think the reported duplicate covers it. – Crowcoder Jan 22 '18 at 16:06
  • Unfortunately, I've already looked through that "duplicate". Anything else I've found on this has been dealt with in javascript / ajax and has things like result.d which I can neither access or see within C#. – iandavidson1982 Jan 22 '18 at 16:11
  • Just refactor your test class so it matches the json schema or use custom json serializer and apply the proper attributes. Using web services means conforming to their response, not the other way around. – Crowcoder Jan 22 '18 at 16:16
  • Ive managed to get it to work by setting the webmethod as void and putting the json into the response manually using HttpContext.Current.Response.Write(json); Thanks – iandavidson1982 Jan 22 '18 at 16:18

0 Answers0