1.先创建一个DynamicContractResolver对象,继承DefaultContractResolver,然后重写CreateProperties方法.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json.Serialization;
namespace JSONDemo
{
public class DynamicContractResolver : DefaultContractResolver
{
private readonly char _firstChar;
public DynamicContractResolver(char firstChar)
{
this._firstChar = firstChar;
}
protected override IList<JsonProperty> CreateProperties(Type type, GongHuiNewtonsoft.Json.MemberSerialization memberSerialization)
{
IList<JsonProperty> propertyList = base.CreateProperties(type, memberSerialization);
//仅序列化首字母相匹配的成员
propertyList = propertyList.Where(p => p.PropertyName.StartsWith(_firstChar.ToString())).ToList();
return propertyList;
}