标题:关于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 13:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-363262-1-1.html




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

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