对返回的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);
        }
    }

 

上一篇:图片上传预加载


下一篇:第22章:针对Cloze Tests基于Attention机制的的MRC领域开山之作:Teaching Machines to Read and Comprehend架构设计及完整源码实现