0

I've a wcf web service that normally returns DataTable. I serialized it with Newtonsoft.JSON but it is returning it in string marks i mean ' " ' marks. And thats why i can not use it in Android.

I've tried return object but nothing changed. I tried change response format or something like it but it is still same.

Here is my WCF Interface Code

[WebGet (BodyStyle = WebMessageBodyStyle.Bare,
        ResponseFormat =WebMessageFormat.Json,
        UriTemplate = "GetRecords")]

Here is my service code.

     public string btGetTable()
     {
        DataTable dt = new DataTable("ExampleDT");
        dt.Columns.Add(new DataColumn("KODU", typeof(string)));
        dt.Columns.Add(new DataColumn("ACIKLAMA", typeof(string)));
        dt.Columns.Add(new DataColumn("KAPALI_FL", typeof(bool)));
        dt.Columns.Add(new DataColumn("TARIH", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("TUTAR", typeof(Decimal)));
        dt.Columns.Add(new DataColumn("DURUM", typeof(short)));

        DataRow dr = dt.NewRow();
        dr[0] = "TLP.00000001";
        dr[1] = "Dummy Record 1";
        dr[2] = false;
        dr[3] = DateTime.Now;
        dr[4] = 1254.78d;
        dr[5] = 0;
        dt.Rows.Add(dr);

 string serializedDt = JsonConvert.SerializeObject(dt);
 return serializedDt;

Here is the actual output

 "[{\"KODU\":\"TLP.00000001\",\"ACIKLAMA\":\"Dummy Record 1\",\"KAPALI_FL\":false,\"TARIH\":\"2018-12-26T11:42:03.2652779\",\"TUTAR\":1254.78,\"DURUM\":0}]"

Expected Output

 [{"KODU":"TLP.00000001","ACIKLAMA":"Dummy Record 1","KAPALI_FL":false,"TARIH":"2018-12-26T11:42:03.2652779","TUTAR":1254.78,"DURUM":0}]
  • I think the main issue is that its a datatable. I would say consider using a custom object or check out this https://stackoverflow.com/questions/17398019/convert-datatable-to-json-in-c-sharp – Ryan Schlueter Dec 26 '18 at 15:42
  • In order to return Json(JavaScript Object Notation) string in http-mode wcf service, we usually return List and specify ResponseFormat=WebMessageFormat.Json. – Abraham Qian Dec 27 '18 at 03:26
  • Sorry for the late response. I've solved it. Thanks everyone for your effort. – Cemal Çelik Dec 27 '18 at 06:50

1 Answers1

0

Is this causing an issue for the client that is consuming your web method?

The backslashes are not really there, if you use the Text Visualizer while debugging you will see the JSON with no backslashes:

[{"KODU":"TLP.00000001","ACIKLAMA":"Dummy Record 1","KAPALI_FL":false,"TARIH":"2018-12-26T12:19:22.6096277","TUTAR":1254.78,"DURUM":0}]

The backslashes are visible/used to denote an escape for the double quotes while being passed in the request.

If you replace all of your double quotes with and empty string the backslashes will not longer be there. i.e. return serializeddt.Replace("\"","");

"[{KODU:TLP.00000001,ACIKLAMA:Dummy Record 1,KAPALI_FL:false,TARIH:2018-12-26T12:44:16.737631,TUTAR:1254.78,DURUM:0}]"

If the backslashes are a games topper then you will probably need to define and object as your response and instead of string, and pass back that object.

Popo
  • 2,402
  • 5
  • 33
  • 55
  • Hi. Thanks for your valuable answer. I know the backslashes are not there. And i solved my problem btw. I added this code snippet and returning Stream from my method. WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"; MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(serializedDt)); – Cemal Çelik Dec 27 '18 at 06:51