list转换json并将其在jquery中解析出来

本文介绍了一个使用C#将List对象转换为JSON字符串的方法。通过反射获取对象属性,并将其格式化为标准JSON格式。文章还展示了如何在网页中调用此方法并显示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using model;
using System.Text;
using System.Reflection;

namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Class1 s = new Class1();
            s.aa = "1111";
            s.bb = "2222";
            s.cc = "3333";

            List<Class1> c = new List<Class1>();
            c.Add(s);

            context.Response.Write(ListToJson(c));
        }


        /// <summary>

        /// List转成json

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="jsonName"></param>

        /// <param name="list"></param>

        /// <returns></returns>

        public static string ListToJson<T>(IList<T> list, string jsonName)
        {

            StringBuilder Json = new StringBuilder();

            if (string.IsNullOrEmpty(jsonName))

                jsonName = list[0].GetType().Name;

            Json.Append("{\"" + jsonName + "\":[");

            if (list.Count > 0)
            {

                for (int i = 0; i < list.Count; i++)
                {

                    T obj = Activator.CreateInstance<T>();

                    PropertyInfo[] pi = obj.GetType().GetProperties();

                    Json.Append("{");

                    for (int j = 0; j < pi.Length; j++)
                    {

                        Type type = pi[j].GetValue(list[i], null).GetType();

                        Json.Append("\"" + pi[j].Name.ToString() + "\":" + String.Format(pi[j].GetValue(list[i], null).ToString(), type));



                        if (j < pi.Length - 1)
                        {

                            Json.Append(",");

                        }

                    }

                    Json.Append("}");

                    if (i < list.Count - 1)
                    {

                        Json.Append(",");

                    }

                }

            }

            Json.Append("]}");

            return Json.ToString();

        }



        /// <summary>

        /// List转成json

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="list"></param>

        /// <returns></returns>

        public static string ListToJson<T>(IList<T> list)
        {
            object obj = list[0];

            return ListToJson<T>(list, obj.GetType().Name);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}




//html


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript">
        $(
            function () {
                jQuery.post('Handler1.ashx', {}, function (data) {

                    var obj = eval("(" + data + ")"); 

                    $.each(obj.Class1, function (index, table) {
                        alert(table.aa);
                    });
                })
            }
        )
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值