C#之Json工具类
封装类:
using PDM.Models;
using Sapi.Services;
using Sapi.Utility;
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using IDPlat.Models;
using System.Web.Script.Serialization;
namespace PDM.Services
{
class JsonHelper
{
public static string Serialize<T>(T t)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(t);
}
public static T Deserialize<T>(string Json)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<T>(Json);
}
public static JsonBuilder CreateJsonObjectBuilder()
{
JsonBuilder builder = new JsonBuilder();
return builder;
}
public static JsonBuilder CreateJsonArrayBuilder()
{
JsonBuilder builder = new JsonBuilder();
return builder.ToArray();
}
}
public class JsonBuilder
{
private JsonBuilder jsonBuilder;
private Dictionary<string, object> JsonObject;
private List<Dictionary<string, object>> JsonArray;
public JsonBuilder()
{
jsonBuilder = this;
JsonObject = new Dictionary<string, object>();
}
public JsonBuilder ToObject()
{
if (JsonArray != null && JsonArray.Count > 0)
{
JsonObject = JsonArray[0];
}
else
{
JsonArray = null;
JsonObject = new Dictionary<string, object>();
}
return jsonBuilder;
}
public JsonBuilder ToArray()
{
JsonArray = new List<Dictionary<string, object>>();
if (JsonObject.Count > 0)
JsonArray.Add(JsonObject);
return jsonBuilder;
}
public bool IsArray()
{
return JsonArray != null;
}
public JsonBuilder SetProperty(string name, object value)
{
if (IsArray())
return jsonBuilder;
if (typeof(JsonBuilder) == value.GetType())
{
JsonBuilder JsonValue = value as JsonBuilder;
if (JsonValue.IsArray())
value = JsonValue.JsonArray;
else
value = JsonValue.JsonObject;
}
//jsonBuilder = (IDictionary<string, object>)jsonBuilder;
if (JsonObject.ContainsKey(name))
{
JsonObject[name] = value;
}
else
{
JsonObject.Add(name, value);
}
return jsonBuilder;
}
public JsonBuilder RemoveProperty(string name)
{
if (IsArray())
return jsonBuilder;
if (JsonObject.ContainsKey(name))
{
JsonObject.Remove(name);
}
return jsonBuilder;
}
public JsonBuilder AddItem(JsonBuilder jb)
{
if (!IsArray())
return jsonBuilder;
if (jb.IsArray())
JsonArray.AddRange(jb.JsonArray);
else
JsonArray.Add(jb.JsonObject);
return jsonBuilder;
}
public JsonBuilder RemoveItem(JsonBuilder jb)
{
if (!IsArray())
return jsonBuilder;
if (jb.IsArray())
{
foreach (var deleItem in jb.JsonArray)
{
if (JsonArray.Contains(deleItem))
JsonArray.Remove(deleItem);
}
}
else
{
if (JsonArray.Contains(jb.JsonObject))
JsonArray.Remove(jb.JsonObject);
}
return jsonBuilder;
}
public string ToJson()
{
object ToSerialize;
if (IsArray())
ToSerialize = jsonBuilder.JsonArray;
else
ToSerialize = jsonBuilder.JsonObject;
return JsonHelper.Serialize<dynamic>(ToSerialize);
}
}
}
测试:
var arraydata = new List<string>();
for (int i=0;i<5;i++)
{
arraydata.Add(str);//str是i转string
}
JsonBuilder json = JsonHelper.CreateJsonObjectBuilder();
JsonBuilder array = JsonHelper.CreateJsonArrayBuilder();
array.AddItem(new JsonBuilder().SetProperty("item1","item1__value"));
array.AddItem(new JsonBuilder().SetProperty("item2","item2__value"));
json = json.SetProperty("name", "Zack").SetProperty("blog","cnblogs").SetProperty("obj", (new JsonBuilder().SetProperty("value", 1000))).SetProperty("array", array).SetProperty("ProjectIds", arraydata);
Console.WriteLine(json.ToJson());
Console.ReadLine();
效果:
{
"name": "Zack",
"blog": "cnblogs",
"obj": {
"value": 1000
},
"array": [{
"item1": "item1__value"
}, {
"item2": "item2__value"
}],
"ProjectIds":["0","1","2","3","4"]
}