我想序列化我班级的所有属性,但希望在返回响应时隐藏一些属性。
我正在使用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; }
}
编辑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"
}
]
编辑2:找到解决方案
我通过一些研究找到了解决方案。
我创建了一个自定义合约解析程序来序列化和反序列化所有属性并将其传递。
以下是我的代码
public class AllPropertiesResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.Ignored = false;
return property;
}
}
下面是我调用它的代码。
JsonConvert.SerializeObject(object, new JsonSerializerSettings() { ContractResolver = new AllPropertiesResolver() });
JsonConvert.DeserializeObject<T>(stream, new JsonSerializerSettings() { ContractResolver = new AllPropertiesResolver() });
谢谢大家的回复。
答案 0 :(得分:4)
您可以使用[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; }
}
输出:
[
{
"placeName": "Malacca"
},
{
"placeName": "Kuala Lumpur"
},
{
"placeName": "Genting Highlands"
},
{
"placeName": "Singapore"
},
{
"placeName": "Penang"
},
{
"placeName": "Perak"
},
{
"placeName": "Selangor"
}
]
或者,如果你不介意一点反思。下面我们使用JsonInclude
- 属性,该属性将覆盖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
{
}
答案 1 :(得分:1)
可能的解决方案之一是使用匿名类:return new { PlaceName = place.PlaceName };
。
另一种解决方案是为您的类型创建自己的序列化程序并将其用于该类型。自定义序列化程序的示例,您可以找到here
答案 2 :(得分:0)
如果您的Json输出是List<Place>
,您可以尝试:
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 Places = JsonConvert.DeserializeObject<List<Place>>(json);
foreach (var place in Places)
{
Console.WriteLine(place.PlaceName);
}