陈斌彬的技术博客

Stay foolish,stay hungry

Webservice 返回批量 JSON 数据(原创)

测试结果

img

img

NET程序

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Text;

namespace WebApplication1
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        #region 平安校园
        [WebMethod(Description = "平安校园列表")]
        [XmlInclude(typeof(ReadCardRecordClass))]
        public string GetCardRecordList()
        {
            //连接SQL数据库
            SqlConnection SqlCnn = new SqlConnection("Data Source=192.168.20.5;Initial Catalog=RjtSchool;User ID=sa;Password=jsb@2015;");
            //打开数据库连接
            SqlCnn.Open();

            //加入SQL语句,实现数据库功能
            SqlDataAdapter SqlCardRecord = new SqlDataAdapter("select * from dbo.hx_tblCardRecord", SqlCnn);
            //创建缓存
            DataSet DSCardRecord = new DataSet();
            //将SQL语句放入缓存
            SqlCardRecord.Fill(DSCardRecord);
            //获取第一张表
            DataTable dt = DSCardRecord.Tables[0];

            SqlDataAdapter SqlGrade = new SqlDataAdapter("select * from dbo.hx_tblGrade", SqlCnn);
            DataSet DSGrade = new DataSet();
            //将SQL语句放入缓存
            SqlGrade.Fill(DSGrade);
            //获取第一张表
            DataTable dtGrade = DSGrade.Tables[0];

            SqlDataAdapter SqlClass = new SqlDataAdapter("select * from dbo.hx_tblClasses", SqlCnn);
            DataSet DSClass = new DataSet();
            //将SQL语句放入缓存
            SqlClass.Fill(DSClass);
            //获取第一张表
            DataTable dtClass = DSClass.Tables[0];

            IList result = new ArrayList();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int ID = (int)dt.Rows[i][0];
                DateTime CreateDate = (DateTime)dt.Rows[i][1];
                bool IsAvailable = (bool)dt.Rows[i][2];
                string StudentName = (string)dt.Rows[i][4];
                string Grade = (string)dt.Rows[i][6];
                int intGrade = Convert.ToInt32(Grade);
                string Classes = (string)dt.Rows[i][7];
                int intClasses = Convert.ToInt32(Classes);
                string GradeName = null;
                string ClassesName = null;
                string InOut = (string)dt.Rows[i][11];

                for (int j = 0; j < dtGrade.Rows.Count; j++)
                {
                    if (intGrade == (int)dtGrade.Rows[j][0])
                    {
                        GradeName = (string)dtGrade.Rows[j][6];
                    }
                }

                for (int j = 0; j < dtClass.Rows.Count; j++)
                {
                    if (intClasses == (int)dtClass.Rows[j][0])
                    {
                        ClassesName = (string)dtClass.Rows[j][7];
                    }
                }

                if (IsAvailable)
                {
                    result.Add(new ReadCardRecordClass(CreateDate, StudentName, GradeName, ClassesName,InOut));

                }
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(result);

            json = Regex.Replace(json, @"\\/Date\((\d+)\)\\/", match =>
            {
                DateTime dtt = new DateTime(1970, 1, 1);
                dtt = dtt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                dtt = dtt.ToLocalTime();
                return dtt.ToString("yyyy-MM-dd HH:mm:ss");
            });
           // Context.Response.ContentType = "application/json;charset = UTF-8";
           // Context.Response.Write(json);
           // Context.Response.End();
           return json;
        }

        public class ReadCardRecordClass
        {
            public DateTime CreateDate;
            public string StudentName;
            public string GradeName;
            public string ClassesName;
            public string InOut;

            public ReadCardRecordClass()
            {

            }
            public ReadCardRecordClass( DateTime CreateDate, string StudentName,string GradeName, string ClassesName, string InOut)
            {
                this.CreateDate = CreateDate;
                this.StudentName = StudentName;
                this.GradeName = GradeName;
                this.ClassesName = ClassesName;
                this.InOut = InOut;
            }

        }


        #endregion
    }
}