zoukankan      html  css  js  c++  java
  • JSON C# Class Generator

    http://www.xamasoft.com/json-class-generator/

    JsonHelper.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.JScript;
    
    namespace Common
    {
        /// <summary> 
        /// Json字符串zhuanh 
        /// </summary> 
        public class JsonHelper
        {
            /// <summary> 
            /// 是否添加get set 
            /// </summary> 
            private bool isAddGetSet = false;
    
            /// <summary> 
            /// 数据集合,临时 
            /// </summary> 
            private List<AutoClass> dataList = new List<AutoClass>();
    
            public JsonHelper()
            {
            }
    
            public JsonHelper(bool isAddGetSet)
            {
                this.isAddGetSet = isAddGetSet;
            }
    
            /// <summary> 
            /// 获取类的字符串形式 
            /// </summary> 
            /// <param name="jsonStr"></param> 
            /// <returns></returns> 
            public string GetClassString(string jsonStr)
            {
                Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
                var m = Microsoft.JScript.Eval.JScriptEvaluate("(" + jsonStr + ")", ve);
    
                int index = 0;
                var result = GetDicType((JSObject)m, ref index);
    
                StringBuilder content = new StringBuilder();
                foreach (var item in dataList)
                {
                    content.AppendFormat("	public class {0}
    ", item.CLassName);
                    content.AppendLine("	{");
                    foreach (var model in item.Dic)
                    {
                        if (isAddGetSet)
                        {
                            content.AppendFormat("		public {0} {1}", model.Value, model.Key);
                            content.Append(" { get; set; }
    ");
                        }
                        else
                        {
                            content.AppendFormat("		public {0} {1};
    ", model.Value, model.Key);
                        }
    
                        content.AppendLine();
                    }
    
                    content.AppendLine("	}");
                    content.AppendLine();
                }
    
                return content.ToString();
            }
    
            /// <summary> 
            /// 获取类型的字符串表示 
            /// </summary> 
            /// <param name="type"></param> 
            /// <returns></returns> 
            private string GetTypeString(Type type)
            {
                if (type == typeof(int))
                {
                    return "int";
                }
                else if (type == typeof(bool))
                {
                    return "bool";
                }
                else if (type == typeof(Int64))
                {
                    return "long";
                }
                else if (type == typeof(string))
                {
                    return "string";
                }
                else if (type == typeof(List<string>))
                {
                    return "List<string>";
                }
                else if (type == typeof(List<int>))
                {
                    return "List<int>";
                }
                else
                {
                    return "string";
                }
            }
    
            /// <summary> 
            /// 获取字典类型 
            /// </summary> 
            /// <returns></returns> 
            private string GetDicType(JSObject jsObj, ref int index)
            {
                AutoClass classInfo = new AutoClass();
    
                var model = ((Microsoft.JScript.JSObject)(jsObj)).GetMembers(System.Reflection.BindingFlags.GetField);
                foreach (Microsoft.JScript.JSField item in model)
                {
                    string name = item.Name;
                    Type type = item.GetValue(item).GetType();
                    if (type == typeof(ArrayObject))
                    {
                        // 集合 
                        string typeName = GetDicListType((ArrayObject)item.GetValue(item), ref index);
                        if (!string.IsNullOrEmpty(typeName))
                        {
                            classInfo.Dic.Add(name, typeName);
                        }
                    }
                    else if (type == typeof(JSObject))
                    {
                        // 单个对象 
                        string typeName = GetDicType((JSObject)item.GetValue(item), ref index);
                        if (!string.IsNullOrEmpty(typeName))
                        {
                            classInfo.Dic.Add(name, typeName);
                        }
                    }
                    else
                    {
                        classInfo.Dic.Add(name, GetTypeString(type));
                    }
                }
    
                index++;
                classInfo.CLassName = "Class" + index;
                dataList.Add(classInfo);
                return classInfo.CLassName;
            }
    
            /// <summary> 
            /// 读取集合类型 
            /// </summary> 
            /// <param name="jsArray"></param> 
            /// <param name="index"></param> 
            /// <returns></returns> 
            private string GetDicListType(ArrayObject jsArray, ref int index)
            {
                string name = string.Empty;
                if ((int)jsArray.length > 0)
                {
                    var item = jsArray[0];
                    var type = item.GetType();
                    if (type == typeof(JSObject))
                    {
                        name = "List<" + GetDicType((JSObject)item, ref index) + ">";
                    }
                    else
                    {
                        name = "List<" + GetTypeString(type) + ">";
                    }
                }
    
                return name;
            }
        }
    
        public class AutoClass
        {
            public string CLassName { get; set; }
    
            private Dictionary<string, string> dic = new Dictionary<string, string>();
    
            public Dictionary<string, string> Dic
            {
                get
                {
                    return this.dic;
                }
                set
                {
                    this.dic = value;
                }
            }
        }
    }
    

      

      JsonHelper helper = new JsonHelper(true);
                try
                {
                    this.richTextBox2.Text = helper.GetClassString(richTextBox1.Text);
                }
                catch
                {
                    this.richTextBox2.Text = "输入内容不符合规范...";
                }

  • 相关阅读:
    c# DES加密解密
    命令行远程调用图形界面程序
    mpv0.29 vo=x11 resize窗口渲染存在不正常黑色显示
    记qt 焦点状态在多个子窗口的关系
    linux_虚拟机终端连接方法
    python_爬虫_微信公众号抓取
    python_爬虫_multiprocessing.dummy以及multiprocessing
    python_爬虫_腾讯新闻app 单页新闻数据分析爬取
    python_爬虫_Charles手机证书安装问题
    python_爬虫_Selenium_Error
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7358849.html
Copyright © 2011-2022 走看看