Sat*_*mar 2 c# serialization json asp.net-web-api
我想序列化我的类的所有属性,但想在返回响应时隐藏一些属性。
我正在使用 NewtonSoft.Json.Net 进行序列化。
例如,在下面的类中,我想序列化这两个属性,但我只想返回 PlaceName。
有没有办法做到这一点?
[DataContract]
public class Place
{
[DataMember(EmitDefaultValue = false)]
public int PlaceId { get; set; }
[DataMember(EmitDefaultValue = false, Order = 1)]
public string PlaceName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
编辑 1:
下面是我当前的 Json 文件。
[
{
"placeId": 1,
"placeName": "Malacca"
},
{
"placeId": 2,
"placeName": "Kuala Lumpur"
},
{
"placeId": 3,
"placeName": "Genting Highlands"
},
{
"placeId": 4,
"placeName": "Singapore"
},
{
"placeId": 5,
"placeName": "Penang"
},
{
"placeId": 6,
"placeName": "Perak"
},
{
"placeId": 8,
"placeName": "Selangor"
}
]
Run Code Online (Sandbox Code Playgroud)
编辑 2:找到解决方案
我通过一些研究找到了解决方案。
我创建了一个自定义合同解析器来序列化和反序列化所有属性并传递它。
下面是我的代码
public class AllPropertiesResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.Ignored = false;
return property;
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我调用它的代码。
JsonConvert.SerializeObject(object, new JsonSerializerSettings() { ContractResolver = new AllPropertiesResolver() });
JsonConvert.DeserializeObject<T>(stream, new JsonSerializerSettings() { ContractResolver = new AllPropertiesResolver() });
Run Code Online (Sandbox Code Playgroud)
谢谢大家的回应。
您可以使用[JsonIgnore]. 由于您用asp.net-web-api我的问题标记了您的问题,我认为您实际上使用了它。因此,下面是一个示例,其中控制器将返回整个模型,但带有JsonIgnore. 通过使用自定义ContractResolver我们序列化它以包含所有属性(即使它们得到JsonIgnore)。并ContractResolver在返回我们的响应时使用默认值。
但请注意,它会覆盖默认行为。因此,您可能想要添加一些其他检查,而不仅仅是设置Ignored = false;
public class PlaceController : ApiController
{
[HttpGet]
public IHttpActionResult Get()
{
var json = "[{\"placeId\": 1,\"placeName\": \"Malacca\"},{\"placeId\": 2,\"placeName\": \"Kuala Lumpur\"},{\"placeId\": 3,\"placeName\": \"Genting Highlands\"},{\"placeId\": 4,\"placeName\": \"Singapore\"},{\"placeId\": 5,\"placeName\": \"Penang\"},{\"placeId\": 6,\"placeName\": \"Perak\"},{\"placeId\": 8,\"placeName\": \"Selangor\"}]";
var settings = new JsonSerializerSettings();
settings.ContractResolver = new IncludeAllPropertiesContractResolver();
var places = JsonConvert.DeserializeObject<Place[]>(json, settings);
return Ok(places);
}
}
public class IncludeAllPropertiesContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
// Or other way to determine...
foreach (var jsonProperty in properties)
{
// Include all properties.
jsonProperty.Ignored = false;
}
return properties;
}
}
[DataContract]
public class Place
{
[JsonIgnore]
[DataMember(EmitDefaultValue = false)]
public int PlaceId { get; set; }
[DataMember(EmitDefaultValue = false, Order = 1)]
public string PlaceName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
输出:
[
{
"placeName": "Malacca"
},
{
"placeName": "Kuala Lumpur"
},
{
"placeName": "Genting Highlands"
},
{
"placeName": "Singapore"
},
{
"placeName": "Penang"
},
{
"placeName": "Perak"
},
{
"placeName": "Selangor"
}
]
Run Code Online (Sandbox Code Playgroud)
或者,如果您不介意进行一点反思。下面我们使用一个JsonInclude-attribute,它将覆盖JsonIgnore.
public class JsonIncludeContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
var actualProperties = type.GetProperties();
foreach (var jsonProperty in properties)
{
// Check if it got our JsonInclude attribute.
var property = actualProperties.FirstOrDefault(x => x.Name == jsonProperty.PropertyName);
if (property != null && property.GetCustomAttribute(typeof(JsonInclude)) != null)
{
jsonProperty.Ignored = false;
}
}
return properties;
}
}
[DataContract]
public class Place
{
[JsonInclude] // Will override JsonIgnore.
[JsonIgnore]
[DataMember(EmitDefaultValue = false)]
public int PlaceId { get; set; }
[DataMember(EmitDefaultValue = false, Order = 1)]
public string PlaceName { get; set; }
}
public class JsonInclude : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2155 次 |
| 最近记录: |