
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.JScript;
6
7 namespace Common
8 {
9 /// <summary>
10 /// Json字符串zhuanh
11 /// </summary>
12 public class JsonHelper : IHelper
13 {
14 /// <summary>
15 /// 是否添加get set
16 /// </summary>
17 private bool isAddGetSet = false;
18
19 /// <summary>
20 /// 数据集合,临时
21 /// </summary>
22 private List<AutoClass> dataList = new List<AutoClass>();
23
24 public JsonHelper()
25 {
26 }
27
28 public JsonHelper(bool isAddGetSet)
29 {
30 this.isAddGetSet = isAddGetSet;
31 }
32
33 /// <summary>
34 /// 获取类的字符串形式
35 /// </summary>
36 /// <param name="jsonStr"></param>
37 /// <returns></returns>
38 public string GetClassString(string jsonStr)
39 {
40 Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
41 var m = Microsoft.JScript.Eval.JScriptEvaluate("(" + jsonStr + ")", ve);
42
43 int index = 0;
44 var result = GetDicType((JSObject)m, ref index);
45
46 StringBuilder content = new StringBuilder();
47 foreach (var item in dataList)
48 {
49 content.AppendFormat("\tpublic class {0}\r\n", item.CLassName);
50 content.AppendLine("\t{");
51 foreach (var model in item.Dic)
52 {
53 if (isAddGetSet)
54 {
55 content.AppendFormat("\t\tpublic {0} {1}", model.Value, model.Key);
56 content.Append(" { get; set; }\r\n");
57 }
58 else
59 {
60 content.AppendFormat("\t\tpublic {0} {1};\r\n", model.Value, model.Key);
61 }
62
63 content.AppendLine();
64 }
65
66 content.AppendLine("\t}");
67 content.AppendLine();
68 }
69
70 return content.ToString();
71 }
72
73 /// <summary>
74 /// 获取类型的字符串表示
75 /// </summary>
76 /// <param name="type"></param>
77 /// <returns></returns>
78 private string GetTypeString(Type type)
79 {
80 if (type == typeof(int))
81 {
82 return "int";
83 }
84 else if (type == typeof(bool))