Dynamic動態解析Json操作用法講解


DynJsonHelper操作用法大全:
1,將對象轉換為json數據
2,將json數據轉為對象
3,將數組List列表對象轉換為json數據
4,將json數據轉為List數組列表對象

DynJsonHelper動態解析Json幫助類源碼下載

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace AutoHome.JsonDynamic
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 將對象轉換為json數據
                //將對象轉換為json數據
                TestInfo info = new TestInfo() { Name = "Json解析", Num = 112, Time = DateTime.Now.ToString("g"), ByteStr = 0 };
                string jsonstr = DynJsonHelper.ToJson(info);//將對象轉換為json數據
                //輸出的json串為:{"Name":"Json解析","Num":112,"Time":"2016-4-1 9:53","ByteStr":0}
                Response.Write("/******************將對象轉換為json數據************************************/"   "<br>");
                Response.Write(jsonstr   "<br><br>");
                #endregion

                #region 將json數據轉為對象
                //將json數據轉為對象
                //可以直接dyninfo.對象名
                jsonstr = "{\"Name\":\"Json解析\",\"Num\":112,\"Time\":\"2016-4-1 9:53\",\"ByteStr\":0}";
                dynamic dyninfo = DynJsonHelper.ToObject(jsonstr);
                Response.Write("/*****************將json數據轉為對象****************************/"   "<br>");
                Response.Write(dyninfo.Name   "-"   dyninfo.Num   "-"   dyninfo.Time   "-"   dyninfo.ByteStr   "<br><br>");
                #endregion

                #region 將數組List列表對象轉換為json數據
                //將數組List列表對象轉換為json數據
                List<TestInfo> list = new List<TestInfo>() {
                    new TestInfo { Name = "Json解析", Num = 112, Time = DateTime.Now.ToString("g"), ByteStr = 0 },
                    new TestInfo { Name = "Json格式化", Num = 113, Time = DateTime.Now.AddDays(-1).ToString("g"), ByteStr = 1 },
                    new TestInfo { Name = "Json序列化", Num = 114, Time = DateTime.Now.AddDays(1).ToString("g"), ByteStr = 2 }};
                string jsonlist = DynJsonHelper.ToJson(list);
                Response.Write("/******************將數組List列表對象轉換為json數據****************************/"   "<br>");
                Response.Write(jsonlist   "<br><br>");
                #endregion

                #region 將json數據轉為List數組列表對象
                //將json數據轉為List數組列表對象
                string newliststr = string.Empty;
                dynamic newlist = DynJsonHelper.ToObject(jsonlist);
                foreach (var item in newlist)
                {
                    newliststr  = item.Name   "-"   item.Num   "-"   item.Time   "-"   item.ByteStr   "<br>";
                }
                Response.Write("/******************將json數據轉為List數組列表對象****************************/"   "<br>");
                Response.Write(newliststr);
                #endregion
            }
        }
        public class TestInfo
        {
            public string Name { get; set; }
            public int Num { get; set; }
            public string Time { get; set; }
            public byte ByteStr { get; set; }
        }
    }
}


原文鏈接:Dynamic動態解析Json操作用法講解