JSON:
JSON - Java Script Object Notation. JSON is light weight data-interchange component.
JSON with Asp.Net Web Services:
.Net Web services returns data on format of SOAP - Xml. JSON is better than compare with Xml. JSON easily parsed by java script. This article provides the solution, how to .net web service returns the format of JSON.
Requirements:
1. Dot Net 2005
2. C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\v1.0.61025
References:
1. Add reference System.Web.Services.
Service.cs:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Collections.Generic;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Service : System.Web.Services.WebService
{
public Service ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string TestJson()
{
Database dc=new Database ();
List
lstjrnlobj = dc.GetJournalData();
return new JavaScriptSerializer().Serialize(lstjrnlobj);
}
}
Database.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Collections;
using System.Collections.Generic;
///
/// Summary description for Database
///
public class Database
{
MySqlConnection conn;
MySqlCommand cmd;
MySqlDataReader dr;
string qry = string.Empty;
public Database()
{
conn = new MySqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
}
public List
{
List
try
{
conn.Open();
qry="select journal,volume,issue,article from workflow where journal='ji' and volume='186' and issue='1'";
cmd = new MySqlCommand(qry, conn);
dr = cmd.ExecuteReader();
while (dr.Read ())
{
JournalObj jobj=new JournalObj ();
jobj.Journal =dr["journal"].ToString ();
jobj.Volume = dr["volume"].ToString();
jobj.Issue = dr["issue"].ToString();
jobj.Articleid = dr["article"].ToString();
lstjrnlobj.Add(jobj);
}
return lstjrnlobj;
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
}
Journalobj.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
///
/// Summary description for JournalObj
///
public class JournalObj
{
public JournalObj()
{
}
private string _journla;
public string Journal
{
get { return _journla; }
set { _journla = value; }
}
private string _volume;
public string Volume
{
get { return _volume; }
set { _volume = value; }
}
private string _issue;
public string Issue
{
get { return _issue; }
set { _issue = value; }
}
private string _articleid;
public string Articleid
{
get { return _articleid; }
set { _articleid = value; }
}
}
JSON Output:
[{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"1001807"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"0903227"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"0903314"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"1000290"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"1000800"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"1002873"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"1002230"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"1090121"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"186_1"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"1001963"},{"Journal":"JI","Volume":"186","Issue":"1","Articleid":"186_1"}]
No comments:
Post a Comment