Wednesday, May 22, 2013

WCF- Rest - JSON - Example



  


JSON (JavaScript Object Notation)



JSON is a lightweight data-interchange format easy to read and write.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application,

JSON's basic types are: Number , String  , Boolean , Array ,  Object,  null
  



Database Table :



CREATE TABLE [dbo].[User_M_FundsCategory](
      [CategoryMain] [varchar](24) COLLATE Latin1_General_CI_AS NOT NULL,
      [FundOwner] [nvarchar](10) COLLATE Latin1_General_CI_AS NULL,
      [FundClass] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [WebFundGroupName] [nvarchar](100) COLLATE Latin1_General_CI_AS NULL,
      [FundID] [int] NOT NULL,
      [FundCode] [nvarchar](10) COLLATE Latin1_General_CI_AS NULL,
      [WebFundName] [nvarchar](102) COLLATE Latin1_General_CI_AS NULL,
      [Sedol] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [Pricing] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [Pricedate] [varchar](30) COLLATE Latin1_General_CI_AS NULL,
      [Bid] [decimal](30, 2) NULL,
      [OfferNAV] [decimal](30, 2) NULL,
      [Hyperlink] [nvarchar](289) COLLATE Latin1_General_CI_AS NULL
)


INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'VAM Fund Pricing', N'VAM', N'VAMClassBC', N'VAM Equity Funds', 15, N'VAM3D', N'US Small Cap Growth B*', N'B10QTB8', N'Daily', N'17/05/2013', CAST(0.00 AS Decimal(30, 2)), CAST(22.00 AS Decimal(30, 2)), N'www.abc.com/abc.pdf')

INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'VAM Fund Pricing', N'VAM', N'VAMClassBC', N'VAM Equity Funds', 44, N'VAMED', N'US Micro Cap Growth B*', N'B10QTG3', N'Daily', N'17/05/2013', CAST(0.00 AS Decimal(30, 2)), CAST(37.01 AS Decimal(30, 2)), N'www.abc.com/abc.pdf')

INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'VAM Fund Pricing', N'VAM', N'VAMClassBC', N'VAM Equity Funds', 45, N'VAMBCD', N'US Large Cap Growth B*', N'B10QTP2', N'Daily', N'17/05/2013', CAST(0.00 AS Decimal(30, 2)), CAST(27.10 AS Decimal(30, 2)), N'www.abc.com/abc.pdf')

INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'VAM Funds (Lux) A Shares', N'VAM', N'VAMClassA', N'VAM Equity Funds', 69, N'VAMMCDA', N'US Mid Cap Growth USD A*', N'B06FNF4', N'Daily', N'17/05/2013', CAST(0.00 AS Decimal(30, 2)), CAST(140.58 AS Decimal(30, 2)), N'')


INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'Mirror Funds', N'Mirror', N'Mirror', N'Hansard International Limited', 142, N'MC137', N'HIL VAM Asia Fixed Income Fund Dollar', N'MC137', N'Weekly', N'15/05/2013', CAST(0.93 AS Decimal(30, 2)), CAST(1.01 AS Decimal(30, 2)), N'')

INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'Mirror Funds', N'Mirror', N'Mirror', N'Old Mutual International', 144, N'OMGBWGF', N'OMGB VAM World Growth Dollar', N'0', N'Daily', N'14/05/2013', CAST(0.65 AS Decimal(30, 2)), CAST(0.65 AS Decimal(30, 2)), N'')

INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'VAM Funds (Lux) A Shares', N'VAM', N'VAMClassA', N'VAM Equity Funds', 72, N'VAMSCDA', N'US Small Cap Growth A*', N'B10QT96', N'Daily', N'17/05/2013', CAST(0.00 AS Decimal(30, 2)), CAST(21.69 AS Decimal(30, 2)), N'')

INSERT [dbo].[User_M_FundsCategory] ([CategoryMain], [FundOwner], [FundClass], [WebFundGroupName], [FundID], [FundCode], [WebFundName], [Sedol], [Pricing], [Pricedate], [Bid], [OfferNAV], [Hyperlink])
VALUES (N'VAM Funds (Lux) A Shares', N'VAM', N'VAMClassA', N'VAM Equity Funds', 73, N'VAMSMDA', N'US Micro Cap Growth A*', N'B10QTF2', N'Daily', N'17/05/2013', CAST(0.00 AS Decimal(30, 2)), CAST(36.72 AS Decimal(30, 2)), N'')




Interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.IO;

public class FundMainCategory
{
    [DataMember(Name = "CategoryName", Order = 1)]
    public string CategoryMain;
}



public class FundSubCategory
{
    [DataMember(Name = "WebFundGroupName", Order = 1)]
    public string WebFundGroupName;
}


public class Funddetails
{
    [DataMember(Name = "CategoryMain", Order = 1)]
    public string CategoryMain;

    [DataMember(Name = "FundOwner", Order = 1)]
    public string FundOwner;

    [DataMember(Name = "FundClass", Order = 2)]
    public string FundClass;

    [DataMember(Name = "WebFundGroupName", Order = 2)]
    public string WebFundGroupName;

    [DataMember(Name = "FundID", Order = 3)]
    public int FundID;

    [DataMember(Name = "FundCode", Order = 4)]
    public string FundCode;

    [DataMember(Name = "WebFundName", Order = 5)]
    public string WebFundName;

    [DataMember(Name = "Sedol", Order = 6)]
    public string Sedol;

    [DataMember(Name = "Pricing", Order = 7)]
    public string Pricing;


    [DataMember(Name = "Pricedate", Order = 8)]
    public string Pricedate;

    [DataMember(Name = "Bid", Order = 9)]
    public string Bid;

    [DataMember(Name = "OfferNAV", Order = 10)]
    public string OfferNAV;

    [DataMember(Name = "Hyperlink", Order = 11)]
    public string Hyperlink;



    [DataMember(Name = "MonthToDate", Order = 12)]
    public string MonthToDate;

    [DataMember(Name = "PreviousMonth", Order = 15)]
    public string PreviousMonth;

    [DataMember(Name = "ThreeMonths", Order = 14)]
    public string ThreeMonths;

    [DataMember(Name = "Currentyear", Order = 16)]
    public string Currentyear;

    [DataMember(Name = "PreviousYear", Order = 13)]
    public string PreviousYear;

    [DataMember(Name = "PreviousYearFrommCurrent", Order = 17)]
    public string PreviousYearFrommCurrent;


    [DataMember(Name = "LastThreeyears", Order = 18)]
    public string LastThreeyears;

    [DataMember(Name = "SinceLaunch", Order = 21)]
    public string SinceLaunch;

    [DataMember(Name = "LastMonthName", Order = 20)]
    public string LastMonthName;

    [DataMember(Name = "YearName", Order = 21)]
    public string YearName;
    [DataMember(Name = "LaunchDate", Order = 22)]
    public string LaunchDate;
}



public class FundPrices
{
    [DataMember(Name = "FundID", Order = 1)]
    public string FundID;

    [DataMember(Name = "OfferPrice", Order = 2)]
    public string OfferPrice;
   
    [DataMember(Name = "PriceDate", Order = 3)]
    public string PriceDate;
}



    [ServiceContract]
    public interface IRestUser
    {

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "getmaincategory", ResponseFormat = WebMessageFormat.Json)]
        List<string> GetMainCategory();

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "getsubsategory/{maincategory}", ResponseFormat = WebMessageFormat.Json)]
        List<string> getGroupNameCategory(string maincategory);

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "getfunddetail/{maincategory}/{subcategory}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        List<Funddetails> getFunds(string maincategory, string subcategory);

      

    }



 Service :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.ServiceModel.Activation;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Web;

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestJsonService : IRestUser
    {
      List<string> _objfundpricesforgraph = new List<string>();

        List<string> _objListMaincat = new List<string>();
        List<string> _objListSubcat = new List<string>();
        List<Funddetails> _objListFund = new List<Funddetails>();
        List<FundPrices> _objListFundPrices = new List<FundPrices>();

        FundMainCategory _objmaincat;
        FundSubCategory _objsubcat;
        Funddetails _objfnd;
        FundPrices _objfndprices;

        public List<string> GetMainCategory()
        {
        FundPriceEntities1 ctx = new FundPriceEntities1();
           
            var em = (from s in ctx.M_FundsCategory
                      orderby s.CategoryMain.Trim() ascending
                      select s.CategoryMain).Distinct();
                      return em.ToList();
        }

        public List<string> getGroupNameCategory(string maincategory)
        {
            FundPriceEntities1 ctx = new FundPriceEntities1();
            var em = (from s in ctx.M_FundsCategory
                      where s.CategoryMain.Trim() == maincategory.Trim()
                      orderby s.WebFundGroupName.Trim() ascending
                      select s.WebFundGroupName.Trim()).Distinct();
                      return em.ToList();// _objListSubcat;
        }


        public List<Funddetails> getFunds(string maincategory, string subcategory)
        {
            FundPriceEntities1 ctx = new FundPriceEntities1();
            var em = from s in ctx.M_FundsCategory
                     where s.CategoryMain.Trim() == maincategory.Trim()
                     && s.WebFundGroupName.Trim() == subcategory.Trim()
                     select s;      
            foreach (M_FundsCategory u in em)
            {
                _objfnd = new Funddetails();
                _objfnd.CategoryMain = u.CategoryMain;
                _objfnd.WebFundGroupName = u.WebFundGroupName;
                _objfnd.FundID = u.FundID;
                _objfnd.FundCode = u.FundCode;
                _objfnd.WebFundName = u.WebFundName;
                _objfnd.Sedol = u.Sedol;
                _objfnd.Pricing = u.Pricing;
                _objfnd.Pricedate = u.Pricedate.ToString();
                _objfnd.Bid = u.Bid.ToString();
                _objfnd.OfferNAV = u.OfferNAV.ToString();
                _objfnd.Hyperlink = u.Hyperlink;
                _objListFund.Add(_objfnd);
            }
            return _objListFund;
        }

    }



 Webconfig File :