我DTO類如何將Json字符串反序列化爲自定義DTO C#對象?
[DataContract]
public class RequestMetaDataXml
{
[DataMember(Name = "Title")]
public string Title { get; set; }
[DataMember(Name = "ExpiryDate")]
public string ExpiryDate { get; set; }
[DataMember(Name = "AllowDownload")]
public string AllowDownload { get; set; }
[DataMember(Name = "IsShare")]
public string IsShare { get; set; }
[DataMember(Name = "IncludeMetadata")]
public string IncludeMetadata { get; set; }
[DataMember(Name = "IsReel")]
public string IsReel { get; set; }
[DataMember(Name = "IsSecuredPublish")]
public string IsSecuredPublish { get; set; }
[DataMember(Name = "Notifications")]
public List<NotificationId> Notifications { get; set; }
[DataMember(Name = "CoverArt")]
public List<CoverArt> Coverart { get; set; }
[DataMember(Name = "ProfileInfo")]
public List<ProfileInfo> Profileinfo { get; set; }
}
[DataContract]
public class NotificationId
{
[DataMember(Name = "Id")]
public string Id { get; set; }
[DataMember(Name = "Type")]
public string Type { get; set; }
}
[DataContract]
public class CoverArt
{
[DataMember(Name = "UploadedFileName")]
public string UploadedFileName { get; set; }
}
[DataContract]
public class ProfileInfo
{
[DataMember(Name = "Watermark")]
public List<WaterMark> WaterMark { get; set; }
}
[DataContract]
public class WaterMark
{
[DataMember(Name = "VideoWatermark")]
public List<VideoWatermark> VideoWatermark { get; set; }
}
[DataContract]
public class VideoWatermark
{
[DataMember(Name = "WaterMarkInfo")]
public List<WaterMarkInfo> WaterMarkInfo { get; set; }
}
[DataContract]
public class WaterMarkInfo
{
[DataMember(Name = "Type")]
public string Type { get; set; }
[DataMember(Name = "FreeText")]
public string FreeText { get; set; }
[DataMember(Name = "Position")]
public string Position { get; set; }
[DataMember(Name = "Size")]
public string Size { get; set; }
[DataMember(Name = "LogoId")]
public string LogoId { get; set; }
}
我有JSON字符串像
"{\"RequestMetaDataXml\":{
\"Title\":\"TestCheck27\",
\"AllowDownload\":true,
\"IsShare\":true,
\"IncludeMetadata\":true,
\"IsReel\":\"false\",
\"IsSecuredPublish\":\"true\",
\"Notifications\":{
\"Id\":\"[email protected]\",
\"Type\":\"ExtUser\"},
\"CoverArt\":{
\"UploadedFileName\":\"\"},
\"ProfileInfo\":{
\"WaterMark\":{
\"VideoWatermark\":{
\"WaterMarkInfo\":{
\"Type\":\"None\",
\"FreeText\":\"\",
\"Position\":
\"MiddleCenter\",
\"Size\":\"Medium\",
\"LogoId\":\"7\"
}
}
}
}
}
}"
我收到JSON序列串這樣我怎樣才能分配到上述DTO? 我想反序列化json字符串,就像它不適合我一樣。
RequestMetaDataXml MetaDataDTO =new RequestMetaDataXml();
MetaDataDTO = JsonConvert.DeserializeObject<RequestMetaDataXml>(arguments["MetaDataDTO"]);
我是generatig的Json在下面的方法
var MetaDataDTO = {
Title:replacedtitle ,
Expirydate: expirydate,
AllowDownload: checkallowdownload,
IsShare: chkAllowShare,
IncludeMetadata: chkincludeMetadata,
IsReel: "false",
IsSecuredPublish: IsSecuredPublish,
Notifications: NotificationId,
CoverArt: { UploadedFileName: UploadedFileName },
ProfileInfo: {
WaterMark: {
VideoWatermark: {
WaterMarkInfo: {
Type : WaterMarkInfoType,
FreeText : WaterMarkText,
Position: WaterMarkPosition,
Size: WaterMarkSize,
LogoId : LogoId
}
}
}
}
}
如何反序列化的JSON值在C#代碼,以及如何將這些值分配到我的自定義DTO?
我要建立像XML數據篩選
<Title></Title>
<ExpiryDate> </ExpiryDate>
<AllowDownload> </AllowDownload>
<IsShare> </IsShare>
<IncludeMetadata> </IncludeMetadata>
<IsReel> </IsReel>
<IsSecuredPublish> </IsSecuredPublish>
<Notifications>
<NotificationId id= type= ></NotificationId>
</Notifications>
<CoverArt>
<UploadedFileName> </UploadedFileName>
</CoverArt>
<ProfileInfo>
<Watermark>
<VideoWatermark>
<WaterMarkInfo>
<Type> </Type>
<FreeText></FreeText>
<Position></Position>
<Size></Size>
<LogoId></LogoId>
</WaterMarkInfo>
</VideoWatermark>
</Watermark>
</ProfileInfo>
我試過這也沒有奏效。 – selvakumar