9月 15

http://tools.wx6.org 新增一个功能

JSON字符串转C#代码

发现已经有一个现成的项目了 

http://jsonclassgenerator.codeplex.com/

不过总觉得这么简单个功能引用这么重的一个外部类库不太好

 

遂自己写了一个,记录下来

 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.Script.Serialization; 
using System.Reflection; 
using Newtonsoft.Json; 
using System.IO; 
using System.Collections; 
  
namespace TestDemo.Web 
{ 
    public partial class JSON : System.Web.UI.Page 
    { 
  
  
        //实例化JavaScriptSerializer类的新实例 
        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); 
  
  
        protected void Page_Load(object sender, EventArgs e) 
        { 
  
            try
            { 
                string json = File.ReadAllText(@"D:\Test\TestDemo\TestDemo.Web\TextFile1.txt"); 
  
                var data = javaScriptSerializer.Deserialize<Dictionary<string, object>>(json); 
                string outKey = string.Empty; 
                GenInfo genInfo = new GenInfo(); 
                genInfo.KeyName = "Root"; 
                this.Gen(data, genInfo, out outKey); 
  
                this.GenCode(genInfo); 
  
   
  
            } 
            catch (Exception ex) 
            { 
                throw new Exception(ex.Message); 
            } 
            Console.Read(); 
  
        } 
  
  
        List<string> result = new List<string>(); 
  
        public void GenCode(GenInfo info) 
        { 
  
            string code = string.Empty; 
            code += "public class " + info.KeyName; 
            code += "{"; 
            foreach (var item in info.Items) 
            { 
                GenCode(item); 
                if (item.KeyType == "field" || item.IsLeaf == true) 
                { 
                    if (item.Items.Count == 0) 
                        code += "public string " + item.KeyName + " { get; set; } "; 
                    else
                        code += "public "+item.KeyName+" " + item.KeyName + " { get; set; } "; 
                } 
                else if (item.KeyType == "list") 
                { 
                    code += "public List<" + item.KeyName + "> " + item.KeyName + " { get; set; } "; 
                } 
            } 
            code += "}"; 
  
            if (info.Items.Count > 0 && info.KeyType != "list") 
                result.Add(code); 
        } 
  
        public void Gen(object obj, GenInfo genInfo, out string childrenKey) 
        { 
  
            childrenKey = string.Empty; 
            if (this.TryConvert2Dic(obj)) 
            { 
                genInfo.KeyType = "field"; 
  
                Dictionary<string, object> dic = (Dictionary<string, object>)(obj); 
                foreach (var dicItem in dic) 
                { 
                    GenInfo gen = new GenInfo(); 
                    gen.KeyName = dicItem.Key; 
                    genInfo.Items.Add(gen); 
                    Gen(dicItem.Value, gen, out childrenKey); 
                } 
            } 
            else if (this.TryConvert2Array(obj)) 
            { 
  
                genInfo.KeyType = "list"; 
                ArrayList array = (ArrayList)obj; 
                GenInfo gen = new GenInfo(); 
                gen.KeyName = genInfo.KeyName; 
                genInfo.Items.Add(gen); 
                Gen(array[0], gen, out childrenKey); 
                //gen.KeyName = childrenKey; 
            } 
            else
            { 
                genInfo.IsLeaf = true; 
            } 
        } 
  
        public bool TryConvert2Dic(object obj) 
        { 
            try
            { 
                Dictionary<string, object> dic = (Dictionary<string, object>)(obj); 
                return true; 
            } 
            catch (Exception ex) 
            { 
                return false; 
            } 
        } 
  
   
  
  
        public bool TryConvert2Array(object obj) 
        { 
            try
            { 
                ArrayList array = (ArrayList)(obj); 
                return true; 
            } 
            catch (Exception ex) 
            { 
                return false; 
            } 
        }  
  
    } 
  
  
    public class GenInfo 
    { 
        public string KeyType { get; set; } 
        public string KeyName { get; set; } 
        public bool IsLeaf { get; set; } 
  
        public List<GenInfo> Items { get; set; } 
        public GenInfo() { this.Items = new List<GenInfo>(); } 
  
    } 
} 

 

written by ocean