1

I need to neglect the default value(*string *) in the output which is displayed when executing the webservice JSON.

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;


namespace Webservice
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        public Service1()
        {
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        //public string GetEmployees(string SearchTerm) 
        public string GetEmployees()
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT *  FROM Contact e ";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.SelectCommand.Connection = con;
            da.Fill(dt);
            con.Close();

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;
            foreach (DataRow rs in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, rs[col]);
                }
                rows.Add(row);
            }

            return "{ \"Cargo\": " + serializer.Serialize(rows) + "}";
        }     

        public string errmsg(Exception ex)
        {
            return "[['ERROR','" + ex.Message + "']]";
        }
    }
}

Output is;

<string>
{ "Cargo": [{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]}
</string>

But the actual output i need is;

  { "Cargo": [{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]}

In the above output i dont want to see the string /string is there is a way to omit that if so please suggest me.

Thanks in advance

Appdev
  • 189
  • 2
  • 6
  • 28

2 Answers2

0

Try replacing return "{ \"Cargo\": " + serializer.Serialize(rows) + "}"; with return serializer.Serialize(new { Cargo = rows });

viki
  • 1,178
  • 1
  • 17
  • 22
  • I tried this but still string and /string is displaying. – Appdev Apr 03 '14 at 13:15
  • Are you using jQuery or javascript function to consume this service? If so, can u pls add that code too in ur question. – viki Apr 04 '14 at 04:45
0

Your question can be found here: ResponseFormat.Json returns xml

And try to use this http://www.nuget.org/packages/Newtonsoft.Json/ it can be very helpful to deal with JSON.

Good Luck.

Community
  • 1
  • 1
Fernando Moreira
  • 785
  • 4
  • 16