views:

49

answers:

2

Hi,

I can't manage to get a proper json response in an asmx web service. Data structure are defined with DataContract and DataMember attribute but specifying DataMember Name doesn't change response data. Do you have any idea? Here the code (it's for jqgrid):

    /// <summary>
        /// a row of the jquery table
        /// </summary>
        [DataContract()]
        public class RowElement
        {
            /// <summary>
            /// an Id for each row
            /// </summary>
            [DataMember(Name = "id")]
            public string Id { get; set; }

            /// <summary>
            /// the table of cells
            /// </summary>
            [DataMember(Name = "cell")]
            public string[] Cell { get; set; }
        }

        /// <summary>
        /// the table for jquery
        /// </summary>
        [DataContract()]
        public class MainGrid
        {
            /// <summary>
            /// the number of pages
            /// </summary>
            [DataMember(Name = "total")]
            public string Total { get; set; }

            /// <summary>
            /// the num of the actual Page?
            /// </summary>
            [DataMember(Name = "page")]
            public string Page { get; set; }

            /// <summary>
            /// nb of elements in all the table
            /// </summary>
            [DataMember(Name = "records")]
            public string Records { get; set; }

            /// <summary>
            /// the list of Rows
            /// </summary>
            [DataMember(Name = "rows")]
            public RowElement[] Rows { get; set; }
        }



    /// <summary>
            /// method for jquery
            /// </summary>
            /// <param name="numRows">nb of Rows</param>
            /// <param name="Page">nb of Page</param>
            /// <param name="sortField">the column for the sort</param>
            /// <param name="sortOrder">the order of the sort</param>
            /// <param name="searchString">the filter if defined</param>
            /// <returns>the grid returned</returns>
            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            ////int Page, int pageSize, string sortIndex, string sortDirection
            public MainGrid jQGridDataASMX(int? numRows, int? page, string sortField, string sortOrder, string searchString)
            {

    ...
MainGrid mg = new MainGrid();
return mg;    
    }

Client side data is still Id and not id.

A: 

I think i found why it doesn't work. It's because ScriptMethod will use JavascriptSerializer and not DataContractJSonSerializer. So i would have to migrate these service to wcf but when i read http://msdn.microsoft.com/en-us/library/bb412168.aspx i see than DataContractJSonSerializer is not JSON-friendly as they said... So if anyone has a solution to do what i try to do with JavascriptSerializer he is welcome.

Vince
A: 

[DataContract] and [DataMember] are part of WCF. They have nothing to do with ASMX web services.

John Saunders