C#超精简的Json工具

  自制了一个C#使用的Json工具类,没有什么奇奇怪怪的引用库,.Net4.0以上可直接运行,适合对json解析有需求但又不需要很重视的项目使用

 话不多说,直接上代码

  1 using System.Collections.Generic;
  2 using System.Web.Script.Serialization;
  3 public class JsonHelper
  4 {
  5     public static string Serialize<T>(T t)
  6     {
  7         JavaScriptSerializer js = new JavaScriptSerializer();
  8         return js.Serialize(t);
  9     }
 10     public static T Deserialize<T>(string Json)
 11     {
 12         JavaScriptSerializer js = new JavaScriptSerializer();
 13         return js.Deserialize<T>(Json);
 14     }
 15     public static JsonBuilder CreateJsonObjectBuilder()
 16     {
 17         JsonBuilder builder = new JsonBuilder();
 18         return builder;
 19     }
 20     public static JsonBuilder CreateJsonArrayBuilder()
 21     {
 22         JsonBuilder builder = new JsonBuilder();
 23         return builder.ToArray();
 24     }
 25 }
 26 public class JsonBuilder
 27 {
 28     private JsonBuilder jsonBuilder;
 29     private Dictionary<string, object> JsonObject;
 30     private List<Dictionary<string, object>> JsonArray;
 31 
 32     public JsonBuilder()
 33     {
 34         jsonBuilder = this;
 35         JsonObject = new Dictionary<string, object>();
 36     }
 37     public JsonBuilder ToObject()
 38     {
 39         if (JsonArray != null && JsonArray.Count > 0)
 40         {
 41             JsonObject = JsonArray[0];
 42         }
 43         else
 44         {
 45             JsonArray = null;
 46             JsonObject = new Dictionary<string, object>();
 47         }
 48         return jsonBuilder;
 49     }
 50     public JsonBuilder ToArray()
 51     {
 52         JsonArray = new List<Dictionary<string, object>>();
 53         if (JsonObject.Count > 0)
 54             JsonArray.Add(JsonObject);
 55         return jsonBuilder;
 56     }
 57     public bool IsArray()
 58     {
 59         return JsonArray != null;
 60     }
 61     public JsonBuilder SetProperty(string name, object value)
 62     {
 63         if (IsArray())
 64             return jsonBuilder;
 65         if (typeof(JsonBuilder) == value.GetType())
 66         {
 67             JsonBuilder JsonValue = value as JsonBuilder;
 68             if (JsonValue.IsArray())
 69                 value = JsonValue.JsonArray;
 70             else
 71                 value = JsonValue.JsonObject;
 72         }
 73         //jsonBuilder = (IDictionary<string, object>)jsonBuilder;
 74         if (JsonObject.ContainsKey(name))
 75         {
 76             JsonObject[name] = value;
 77         }
 78         else
 79         {
 80             JsonObject.Add(name, value);
 81         }
 82 
 83         return jsonBuilder;
 84 
 85     }
 86     public JsonBuilder RemoveProperty(string name)
 87     {
 88         if (IsArray())
 89             return jsonBuilder;
 90         if (JsonObject.ContainsKey(name))
 91         {
 92             JsonObject.Remove(name);
 93         }
 94         return jsonBuilder;
 95     }
 96     public JsonBuilder AddItem(JsonBuilder jb)
 97     {
 98         if (!IsArray())
 99             return jsonBuilder;
100         if (jb.IsArray())
101             JsonArray.AddRange(jb.JsonArray);
102         else
103             JsonArray.Add(jb.JsonObject);
104         return jsonBuilder;
105     }
106     public JsonBuilder RemoveItem(JsonBuilder jb)
107     {
108         if (!IsArray())
109             return jsonBuilder;
110         if (jb.IsArray())
111         {
112             foreach (var deleItem in jb.JsonArray)
113             {
114                 if (JsonArray.Contains(deleItem))
115                     JsonArray.Remove(deleItem);
116             }
117         }
118         else
119         {
120             if (JsonArray.Contains(jb.JsonObject))
121                 JsonArray.Remove(jb.JsonObject);
122         }
123         return jsonBuilder;
124     }
125     public string ToJson()
126     {
127         object ToSerialize;
128         if (IsArray())
129             ToSerialize = jsonBuilder.JsonArray;
130         else
131             ToSerialize = jsonBuilder.JsonObject;
132         return JsonHelper.Serialize<dynamic>(ToSerialize);
133     }
134 }
View Code

下面是测试,受Unity中DoTween动画插件的启发,设计了这种链式开火车的API,用起来可是十分优雅

1 JsonBuilder json = JsonHelper.CreateJsonObjectBuilder();
2 JsonBuilder array = JsonHelper.CreateJsonArrayBuilder();
3 array.AddItem(new JsonBuilder().SetProperty("item1","item1__value"));
4 array.AddItem(new JsonBuilder().SetProperty("item2","item2__value"));
5 json = json.SetProperty("name", "Zack").SetProperty("blog","cnblogs").SetProperty("obj", (new JsonBuilder().SetProperty("value", 1000))).SetProperty("array", array);
6 Console.WriteLine(json.ToJson());
7 Console.ReadLine();

 

结果:

{"name":"Zack","blog":"cnblogs","obj":{"value":1000},"array":[{"item1":"item1__value"},{"item2":"item2__value"}]}

 

当然咯,以上工具类只是我个人用的,还没有在正式项目中验证过,若有什么错误的地方欢迎指正

以上。

posted @ 2020-06-13 15:24  Zack__zhang  阅读(1284)  评论(0编辑  收藏  举报