| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 640 人关注过本帖
标题:list集合转换为json数据
只看楼主 加入收藏
zhangfu_772
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-3-15
收藏
 问题点数:0 回复次数:0 
list集合转换为json数据
<%@ WebHandler Language="C#" Class="MailReader" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

public class MailReader : IHttpHandler {


    List<int> list = null;
    string jsonName = "Source";
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //OATableAdapters.MailTableAdapter ada = new OATableAdapters.MailTableAdapter();        
        //ada.GetData().WriteXml(@"c:\mail.xml");

        //OATableAdapters.UserListTableAdapter ada = new OATableAdapters.UserListTableAdapter();
        //ada.GetData().WriteXml(@"c:\user.xml");

        //代办总条数
        OATableAdapters.DataTable1TableAdapter db = new OATableAdapters.DataTable1TableAdapter();
        OATableAdapters.DataTable1TableAdapter sp = new OATableAdapters.DataTable1TableAdapter();
        OATableAdapters.DataTable1TableAdapter zh = new OATableAdapters.DataTable1TableAdapter();

        int daiban = (int)db.ScalarQuery();
        int shenpi = (int)sp.ShenPi();
        int zhonghe = (int)zh.ZongHe();
        
        list = new List<int>();
        list.Add(daiban);
        list.Add(shenpi);
        list.Add(zhonghe);


        string sdas = ObjectToJson<int>(jsonName, list);
    }
  public string ObjectToJson<T>(string jsonName, List<T> IL)
    {
      StringBuilder Json = new System.Text.StringBuilder();
      Json.Append("{\"" + jsonName + "\":[");
        if (IL.Count > 0)
        {
            for (int i = 0; i < IL.Count; i++)
            {
                Object obj = Activator.CreateInstance<int>();

                PropertyInfo[] pis = obj.GetType().GetProperties();
                Json.Append("{");
                for (int j = 0; j < pis.Length; j++)
                {
                    Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\"");
                    if (j < pis.Length - 1)
                    {
                        Json.Append(",");
                    }
                }
                Json.Append("}");
                if (i < IL.Count - 1)
                {
                    Json.Append(",");
                }
            }
        }
        Json.Append("]}");
        return Json.ToString();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
搜索更多相关主题的帖子: 数据 void public null 
2012-03-15 14:00
快速回复:list集合转换为json数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029184 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved