ASP.NET输出Json格式数据

        protected void Page_Load(object sender, EventArgs e)
        {
            //ASP.NET输出JSON格式数据示例
            //1:自带的JavaScriptSerializer来生成JSON数据
            IList UserList = new List();
            for (int i = 0; i < 3; i++)
            {
                UserModels user = new UserModels();
                user.userId = i + 1;
                user.userName = "ZhangSan";
                user.userPass = "12345678";
                UserList.Add(user);
            }
            JavaScriptSerializer ser = new JavaScriptSerializer();
            String jsonStr = ser.Serialize(UserList);
            Response.Write(jsonStr); 
        }











namespace Systemjson
{
    public class JsonHelper
    {
        ///  
        /// 对象转JSON 
        ///  
        /// 对象 
        /// JSON格式的字符串 
        public static string ObjectToJson(object obj)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                return jss.Serialize(obj);
            }
            catch (Exception ex)
            {

                throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
            }
        }

        ///  
        /// 数据表转键值对集合  
        /// 把DataTable转成 List集合, 存每一行 
        /// 集合中放的是键值对字典,存每一列 
        ///  
        /// 数据表 
        /// 哈希表数组 
        public static List> DataTableToList(DataTable dt)
        {
            List> list = new List>();

            foreach (DataRow dr in dt.Rows)
            {
                Dictionary dic = new Dictionary();
                foreach (DataColumn dc in dt.Columns)
                {
                    dic.Add(dc.ColumnName, dr[dc.ColumnName]);
                }
                list.Add(dic);
            }
            return list;
        }

        ///  
        /// 数据集转键值对数组字典 
        ///  
        /// 数据集 
        /// 键值对数组字典 
        public static Dictionary>> DataSetToDic(DataSet ds)
        {
            Dictionary>> result = new Dictionary>>();

            foreach (DataTable dt in ds.Tables)
                result.Add(dt.TableName, DataTableToList(dt));

            return result;
        }

        ///  
        /// 数据表转JSON 
        ///  
        /// 数据表 
        /// JSON字符串 
        public static string DataTableToJson(DataTable dt)
        {
            return ObjectToJson(DataTableToList(dt));
        }

        ///  
        /// JSON文本转对象,泛型方法 
        ///  
        /// 类型 
        /// JSON文本 
        /// 指定类型的对象 
        public static T JsonToObject(string jsonText)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                return jss.Deserialize(jsonText);
            }
            catch (Exception ex)
            {
                throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
            }
        }

        ///  
        /// 将JSON文本转换为数据表数据 
        ///  
        /// JSON文本 
        /// 数据表字典 
        public static Dictionary>> TablesDataFromJson(string jsonText)
        {
            return JsonToObject>>>(jsonText);
        }

        ///  
        /// 将JSON文本转换成数据行 
        ///  
        /// JSON文本 
        /// 数据行的字典 
        public static Dictionary DataRowFromJson(string jsonText)
        {
            return JsonToObject>(jsonText);
        }
    }
}



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

        [WebMethod]
        public void HelloWorld()
        {
            UserModels user = new UserModels();
            user.userId = 1;
            user.userName = "11";
            user.userPass = "111";
            Context.Response.Write(JsonHelper.ObjectToJson(user));
            Context.Response.End();
            //不需要返回值
            //return JsonHelper.ObjectToJson(user);
        }
    }
}



你可能感兴趣的:(C#—ASP.NET)