You must Sign In to post a response.
  • Category: Webservices

    Json data using Web services

    Hi All,

    I am new to Web services part in Dot Net, My query is, I have to write a service.asmx file which selects data from sql database and saves the same in Json format.

    Code with Sample is appreciated.

    Thanks all
  • #756291
    Code snippet for JSON serializer is defined in web.config

    <configuration>
    <system.web.extensions>
    <scripting>
    <webServices>
    <jsonSerialization maxJsonLength="5000"/>
    </webServices>
    </scripting>
    </system.web.extensions>
    </configuration>

  • #756295
    Thanks for replying.....below is the code of my service.asmx file, in this i can get the data from the database, but i don't have the idea that how to show the data in Json format. Pls guide me with a solution for this.

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Web.Script.Services;
    using System.Xml.Linq;
    using System.Collections.Generic;
    using System.Data.Sql;
    using System.Data.SqlClient;

    namespace JSONWeb
    {
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]

    // 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
    {
    [WebMethod]
    public DataSet SelectData()
    {
    SqlConnection con = new SqlConnection("Data Source=HOME;database=Data;Integrated Security=true");
    SqlCommand cmd = null;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    try
    {
    con.Open();
    cmd = new SqlCommand("SelectData", con);
    cmd.CommandType = CommandType.StoredProcedure;
    da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    con.Close();
    }
    return ds;
    }
    }
    }

  • #756301
    Check this sample to create an object in JSOn format and then Desrialize it



    public string Serialize(object value)
    {
    Type type = value.GetType();

    Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();

    json.NullValueHandling = NullValueHandling.Ignore;

    json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
    json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
    json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    if (type == typeof(DataRow))
    json.Converters.Add(new DataRowConverter());
    else if(type == typeof(DataTable))
    json.Converters.Add(new DataTableConverter());
    else if (type == typeof(DataSet))
    json.Converters.Add(new DataSetConverter());

    StringWriter sw = new StringWriter();
    Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
    if (this.FormatJsonOutput)
    writer.Formatting = Formatting.Indented;
    else
    writer.Formatting = Formatting.None;

    writer.QuoteChar = '"';
    json.Serialize(writer, value);

    string output = sw.ToString();
    writer.Close();
    sw.Close();

    return output;
    }




    public object Deserialize(string jsonText, Type valueType)
    {
    Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();

    json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
    json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
    json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
    json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

    StringReader sr = new StringReader(jsonText);
    Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
    object result = json.Deserialize(reader, valueType);
    reader.Close();

    return result;
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM


  • Sign In to post your comments