Newtonsoft.Json实现Json格式的输出

2015-06-16 09:22:12  访问(1830) 赞(0) 踩(0)


相关下载:Newtonsoft.JsonV4.5.5.14908.zip     

  • using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    
    namespace SlowX.NewtonsoftJsonApp.Classes.Classes
    {
        /// <summary>
        /// Json 数据
        /// </summary>
        [Serializable]
        public class JsonSelectItem
        {
            /// <summary>
            /// Json 数据
            /// </summary>
            public JsonSelectItem()
            {
    
            }
    
            /// <summary>
            /// id
            /// </summary>
            protected string m_id = "";
    
            /// <summary>
            /// id
            /// </summary>
            public string id
            {
                get
                {
                    return m_id;
                }
                set
                {
                    m_id = value;
                }
            }
    
    
            /// <summary>
            /// text
            /// </summary>
            protected string m_text = "";
    
            /// <summary>
            /// text
            /// </summary>
            public string text
            {
                get
                {
                    return m_text;
                }
                set
                {
                    m_text = value;
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static List<JsonSelectItem> BuildListJsonSelectItem(DataTable dt)
            {
                if (dt == null)
                    return null;
    
                List<JsonSelectItem> theResult = new List<JsonSelectItem>();
    
                foreach (DataRow dr in dt.Rows)
                {
                    theResult.Add(new JsonSelectItem(dr));
                }
    
                return theResult;
            }
        }
    }
    
    
  • try
                {
                    List<JsonSelectItem> theList = new List<JsonSelectItem>();
                    JsonSelectItem item = null;
    
                    string strId = null;
                    string strTheName = null;
    
                    strId = comboBox_ID1.Text;
                    strTheName = comboBox_TheName1.Text;
                    item = new JsonSelectItem(strId, strTheName);
                    theList.Add(item);
    
                    strId = comboBox_ID2.Text;
                    strTheName = comboBox_TheName2.Text;
                    item = new JsonSelectItem(strId, strTheName);
                    theList.Add(item);
    
                    strId = comboBox_ID3.Text;
                    strTheName = comboBox_TheName3.Text;
                    item = new JsonSelectItem(strId, strTheName);
                    theList.Add(item);
    
                    string theResult = Newtonsoft.Json.JsonConvert.SerializeObject
                        (
                            theList
                        );
    
                    richTextBox1.Text = theResult;
                }
                catch (Exception err)
                {
                    MsgForm.MsgShowDialog(err.Message);
                }
    

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)