json返回数据多个是数组,单个就不是处理方案
/// <summary>
/// 计算方案 当前返回的对象
/// </summary>
[JsonConverter(typeof(RatedpackageConverter))]
public List<Ratedpackage> RatedPackage { get; set; }
public class RatedpackageConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType.Equals(JsonToken.StartArray))
{
JArray jArray = JArray.Load(reader);
var obj = serializer.Deserialize<List<Ratedpackage>>(jArray.CreateReader());
return obj;
}
else
{
JObject jArray = JObject.Load(reader);
var obj = serializer.Deserialize<Ratedpackage>(jArray.CreateReader());
return new List<Ratedpackage> { obj };
}
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<Ratedpackage>) || objectType == typeof(Ratedpackage);
}
}