其他分享
首页 > 其他分享> > 对返回的json数据重写格式,用特性JsonConverter

对返回的json数据重写格式,用特性JsonConverter

作者:互联网

继承JsonConverter特性

public class ObjJsonConverter<T> : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
             return objectType == typeof(T);
        }
        //读取
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType.Equals(JsonToken.String))
            {
                string data = reader.ToString();//读取字符串,然后解密
                JObject jArray = JObject.FromObject(data);
                var obj = serializer.Deserialize<T>(jArray.CreateReader());
                return obj;
            }
            else
            {
                JObject jArray = JObject.Load(reader);
                var obj = serializer.Deserialize<T>(jArray.CreateReader());
                return obj;
            }
        }
       //写数据
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteValue(value);
        }
    }

 

标签:JsonConverter,obj,JObject,jArray,json,reader,重写,public,serializer
来源: https://www.cnblogs.com/shuaimeng/p/15503593.html