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 = "输入内容不符合规范...";
                }

  • 相关阅读:
    PCB电路板元器件布局的一般原则*(转)
    PCB Layout初学者必会知识总结(转)
    数字器件和模拟器件?
    同一原理图中怎么区分数字电路和模拟电路
    oracle 11g R2执行INSERT语句,数据库把一个汉字看做3个汉字
    SQL存储过程与函数的区别
    用户自定义函数——Oracle 11g R2
    提高使用SQL Developer进行PL/SQL编程的效率——Oracle 11g R2
    Oracle查看用户使用的表
    JAVA-Eclipse快捷键
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7358849.html
Copyright © 2011-2022 走看看