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("\tpublic class {0}\r\n", item.CLassName);
content.AppendLine("\t{");
foreach (var model in item.Dic)
{
if (isAddGetSet)
{
content.AppendFormat("\t\tpublic {0} {1}", model.Value, model.Key);
content.Append(" { get; set; }\r\n");
}
else
{
content.AppendFormat("\t\tpublic {0} {1};\r\n", model.Value, model.Key);
} content.AppendLine();
} content.AppendLine("\t}");
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 = "输入内容不符合规范...";
            }

JSON C# Class Generator的更多相关文章

  1. .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator

    去年,我在一篇文章用原始方法解析复杂字符串,json一定要用JsonMapper么?中介绍了简单的JSON解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...

  2. JSON C# Class Generator ---由json字符串生成C#实体类的工具(转)

    转载地址:http://www.cnblogs.com/finesite/archive/2011/07/31/2122984.html json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但 ...

  3. JSON C# Class Generator是一个从JSON文本中生成C#内的应用程序

    JSON C# Class Generator是一个从JSON文本中生成C#内的应用程序 .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator   ...

  4. JSON C# Class Generator ---由json字符串生成C#实体类的工具

    json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但在服务器端编程过程中,我们常常希望能通过智能提示来提高编码效率.JSON C# Class Generator 能将json格式所表示的J ...

  5. c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具

    c#实例化继承类,必须对被继承类的程序集做引用   0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...

  6. [工具]json转类

    摘要 这周在园子看到一篇介绍JsonCSharpClassGenerator这个工具的文章,感觉挺实用的,在现在项目中json用的是最多的,所以在转换对应的类的时候,确实挺频繁,所以就研究了一下这个工 ...

  7. 由json字符串生成C#实体类的工具

    json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但在服务器端编程过程中,我们常常希望能通过智能提示来提高编码效率.JSON C# Class Generator 能将json格式所表示的J ...

  8. 介绍4款json的java类库 及 其性能测试

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  9. jackson java对象和json对象的互相转换

    概述 Jackson框架是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”. Jackson框架包含了3个核心库:streaming,databind,annotation ...

随机推荐

  1. idea在使用git clone 时出现Filename too long

    idea在使用git clone 时出现Filename too long的报错信息,使用如下命令就可以解决该问题:在 git bash命令模式下,运行命令 git config --global c ...

  2. Spring Boot源码中模块详解

    Spring Boot源码中模块详解 一.源码 spring boot2.1版本源码地址:https://github.com/spring-projects/spring-boot/tree/2.1 ...

  3. curl 使用手册

    curl.1 the man page Related: Manual FAQ HTTP Scripting NAME curl - transfer a URL SYNOPSIS curl [opt ...

  4. pytorch ImageFolder的覆写

    在为数据分类训练分类器的时候,比如猫狗分类时,我们经常会使用pytorch的ImageFolder: CLASS torchvision.datasets.ImageFolder(root, tran ...

  5. [转]图片Premultiplied Alpha到底是干嘛用的

    Premultiplied Alpha 这个概念做游戏开发的人都不会不知道.Xcode 的工程选项里有一项 Compress PNG Files,会对 PNG 进行 Premultiplied Alp ...

  6. 【Js】单页面多个倒计时问题

    代码: <!DOCTYPE html> <html> <head> <title>多个timeout</title> <script ...

  7. Java8 Collectors类的静态工厂方法

    预定义收集器的功能,就是那些可以从Collectors类提供的工厂方法(例如grouping By)创建的收集器. 它们主要提供了三大功能: •将流元素归约和汇总为一个值 •元素分组 •元素分区 •c ...

  8. 【翻译】Flink Table Api & SQL — 自定义 Source & Sink

    本文翻译自官网: User-defined Sources & Sinks  https://ci.apache.org/projects/flink/flink-docs-release-1 ...

  9. C#程序打包安装部署之添加注册表项(转)

    今天为大家整理了一些怎样去做程序安装包的具体文档,这些文档并不能确保每个人在做安装包的时候都能正确去生成和运行,但是这些文档的指导作用对于需要的朋友来说还是很有必要的,在实际产品的安装部署过程中可能有 ...

  10. chrome/firefox如何设置很色背景

    firefox 菜单栏设置黑色 自带黑色主题,直接Enabled 页面背景设置黑色 下载扩展Extensions:Dark Reader chrome 菜单栏设置黑色 下载主题Theme:Just b ...