苏飞论坛

标题: 记录一个Json反射为对象的例子 [打印本页]

作者: 站长苏飞    时间: 2014-8-15 17:05
标题: 记录一个Json反射为对象的例子
[C#] 纯文本查看 复制代码

namespace Tester
{

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            sb.Append("{\"node\":{\"companyId\":230569695,\"levelCode\":\"2,,\",\"text\":\"001\",\"value\":300648756},\"branch\":\"ajaxImageGroup.htm?event=listSubGroups&levelCode=2,,&value=300648756&text=001\"},");
            sb.Append("{\"node\":{\"companyId\":230569695,\"levelCode\":\"4,,\",\"text\":\"artificial leather\",\"value\":301440617},\"branch\":\"ajaxImageGroup.htm?event=listSubGroups&levelCode=4,,&value=301440617&text=artificial leather\"},");
            sb.Append("{\"node\":{\"companyId\":230569695,\"levelCode\":\"1,,\",\"text\":\"leather\",\"value\":300388275},\"branch\":\"ajaxImageGroup.htm?event=listSubGroups&levelCode=1,,&value=300388275&text=leather\"},");
            sb.Append("{\"node\":{\"companyId\":230569695,\"levelCode\":\"3,,\",\"text\":\"shoe leatther\",\"value\":301420981},\"branch\":\"ajaxImageGroup.htm?event=listSubGroups&levelCode=3,,&value=301420981&text=shoe leatther\"}");
            sb.Append("]");

            List<Data> list = new List<Data>();

            list = (List<Data>)JsonHelper.jsonDes<List<Data>>(sb.ToString());
        }
    }

    public class Data
    {
        public NodeInfo node { get; set; }
        public string branch { get; set; }
    }

    public class NodeInfo
    {
        public string companyId { get; set; }
        public string levelCode { get; set; }
        public string text { get; set; }
        public string value { get; set; }
    }
}






欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4