using Fizzler; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; namespace DS.WMS.PrintApi.Utils { public class SemicolonConverter : JsonConverter { public override bool CanConvert(Type objectType) { // 指定转换器适用的类型 return typeof(string).IsAssignableFrom(objectType); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // 读取JSON时不需要进行任何操作,因为我们是进行序列化 throw new NotImplementedException(); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { //new一个JObject对象,JObject可以像操作对象来操作json var newStr = string.Empty; var temp = (string)value; if (temp != null && temp.Contains(";")) { // 写入JSON时,将分号替换为其他字符 string sanitizedValue = temp.Replace(";", ";"); writer.WriteValue(sanitizedValue); } else { writer.WriteValue(value); } } } }