dayat
dayat

Reputation: 15

Create JSON Object in Web Service C#

Im beginner for Web Service in C# to result data JSON. So, I have Web service to throw data with format JSON, this is my result JSON :

[{"RESULT":"2","TKN":"E952B4C5FA9","URL_HOME":"My_Url_Home"}]

How to remove Symbol "[" and "]" in My Result Json, So I want My Result JSON become this :

{"RESULT":"2","TKN":"E952B4C5FA9","URL_HOME":"My_Url_Home"}

And this is My Code :

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void Create_JSON()
    {
        SqlConnection con = new SqlConnection(consString);
        SqlCommand cmd = new SqlCommand();

        DataTable dt;
        SqlDataReader reader;

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            //These headers are handling the "pre-flight" OPTIONS call sent by the browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
        String resultJSON = "";
        JavaScriptSerializer js = new JavaScriptSerializer();
        try
        {
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "My_Store_Procedure";
            cmd.Connection = con;

            con.Open();
            reader = cmd.ExecuteReader();
            dt = new DataTable();
            dt.Load(reader);
            con.Close();


            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<String, Object>> tableRows = new List<Dictionary<string, object>>();
            Dictionary<String, Object> row;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col].ToString());
                }
                tableRows.Add(row);
            }
            resultJSON = serializer.Serialize(tableRows).ToString();

        }
        catch (Exception ex)
        {
            resultJSON = ex.Message.ToString();
        }
        Context.Response.Write(resultJSON);
    }

;

Please Help, and Thanks For All Answer.

Upvotes: 0

Views: 1546

Answers (2)

Eva Lai
Eva Lai

Reputation: 98

I don't know why you want to remove "[" and "]", cause [] are necessary for JSON.
If you have to remove [], then try this

resultJSON = serializer.Serialize(tableRows).ToString();
resultJSON = resultJSON.Substring(1, resultJSON.Length - 2);

Upvotes: 0

temmyraharjo
temmyraharjo

Reputation: 76

Should be like this right?

resultJSON = serializer.Serialize(tableRows[0]).ToString();

The problem is you tableRows type is List, but you expect to be an object.

Upvotes: 1

Related Questions