0

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.

2
  • 1
    [] denotes an array. Commented Jan 18, 2018 at 1:52
  • If you remove [ and ] you won't have valid json anymore. You are serializing a list, a list will serialize to a json array and thus have angle brackets around it. Commented Jan 18, 2018 at 9:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.