6

I am having restful API which is written in C# using dot net framework 4.5..Currently its working alright... i'm returning a result after a JSON conversion.. I'm expecting a pure JSON result... which i'm not getting currently.. I'm expecting simple solution to omit the string XMLNS at the root element where i return the JSON... Result i'am getting: result

My code :

public String GetAllSalesInvoices(string customer_id, string Startdate, string Enddate)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        string query = "SELECT * FROM sales_invoice WHERE customer_id =" + customer_id + " AND invoice_date BETWEEN '" + Startdate + "' AND '" + Enddate + "'";
        DataSet ds = conObj.execQuery(query);
        DataTable dt = new DataTable();
        dt = ds.Tables[0];


        List<sales_invoice> result = new List<sales_invoice>();

        foreach (DataRow dr in dt.Rows)
        {
            sales_invoice inv = new sales_invoice()
            {
                Invoice_id = Convert.ToInt32(dr["invoice_id"]),
                Invoice_date = Convert.ToString(dr["invoice_date"].ToString()),
                Customer_id = Convert.ToInt32(dr["customer_id"]),
                Product_id = Convert.ToInt32((dr["product_id"])),
                Time = Convert.ToString((dr["time"]).ToString()),
                Quantity = Convert.ToInt32((dr["quantity"])),
                Unit_of_measure = Convert.ToString(dr["unit_of_measure"]),
                Product_price = Convert.ToInt32((dr["product_price"])),
                Sub_total = Convert.ToInt32((dr["sub_total"])),
            };
            result.Add(inv);
        }
        string json=serializer.Serialize(result);
        return json;
}

Thanks

dbc
  • 104,963
  • 20
  • 228
  • 340
Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42
  • I'm expecting some code you wrote... Please update your question. – FIL Nov 16 '17 at 12:53
  • 1
    Are you using web api? If so - https://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome – Steve Nov 16 '17 at 13:08
  • No i am having restful web service@Steve – Selaka Nanayakkara Nov 16 '17 at 19:14
  • If you are not using web api then what are you using? [tag:wcf]? – dbc Nov 16 '17 at 20:27
  • yes i am using wcf rest @dbc – Selaka Nanayakkara Nov 16 '17 at 20:50
  • 1
    Your problem is that you are double-serializing your `result`: first you serialize to a JSON string and then WCF serializes the string to XML. Instead, let WCF serialize your `result` for you by simply returning it directly. Then if you want to configure WCF to return JSON see maybe [How to return Json from WCF Service?](https://stackoverflow.com/q/1830459/3744182) or [WCF REST return single method as JSON and XML](https://stackoverflow.com/q/15060089/3744182). – dbc Nov 16 '17 at 22:01
  • Thanks mate.. Got it fixed thanks to you... @dbc – Selaka Nanayakkara Nov 16 '17 at 22:11
  • 1
    Make it an answer then? – dbc Nov 16 '17 at 22:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159195/discussion-between-selaka-nanayakkara-and-dbc). – Selaka Nanayakkara Nov 17 '17 at 03:57

1 Answers1

0

Hi guys i finally managed to come up with an answer for removing the xml namespaces or string namespaces in return. Solution is to use the context method and write content directly to browser it self. For that you can use this code.

String result =js.Serialize(data);
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(result);

This will write json serialized raw data in to the browser without any namespaces. Note that it cannot be used in context of restful api as the this might be different there.

Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42