我有DTO课程
[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 String一样的
"{\"RequestMetaDataXml\":{
\"Title\":\"TestCheck27\",
\"AllowDownload\":true,
\"IsShare\":true,
\"IncludeMetadata\":true,
\"IsReel\":\"false\",
\"IsSecuredPublish\":\"true\",
\"Notifications\":{
\"Id\":\"checktest1@ssss.com\",
\"Type\":\"ExtUser\"},
\"CoverArt\":{
\"UploadedFileName\":\"\"},
\"ProfileInfo\":{
\"WaterMark\":{
\"VideoWatermark\":{
\"WaterMarkInfo\":{
\"Type\":\"None\",
\"FreeText\":\"\",
\"Position\":
\"MiddleCenter\",
\"Size\":\"Medium\",
\"LogoId\":\"7\"
}
}
}
}
}
}"
我收到了Json序列化的字符串,我怎么能分配到Above DTO? 我试图反序列化json字符串,因为它不适合我。
RequestMetaDataXml MetaDataDTO =new RequestMetaDataXml();
MetaDataDTO = JsonConvert.DeserializeObject<RequestMetaDataXml>(arguments["MetaDataDTO"]);
我通过以下方法生成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
}
}
}
}
}
如何在c#代码中反序列化Json的值以及如何将这些值分配到我的自定义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>
答案 0 :(得分:2)
让生活更轻松,并使用库为您进行反序列化。一个通常推荐的,我使用的是Newtonsoft.Json,反序列化就像这样简单:
JsonConvert.DeserializeObject<RequestMetaDataXml>(jsonStringReceived);
答案 1 :(得分:0)
你要修改一件事,json或DTO类,因为json有WaterMark
,VideoWatermark
等单个对象,而DTO类说这些是List
类型对象。此外,您需要一个包装类,因为RequestMetaDataXml
包装在另一个对象中。
包装类:
[DataContract]
public class Wrapper
{
[DataMember(Name = "RequestMetaDataXml")]
public RequestMetaDataXml RequestMetaDataXml { get; set; }
}
以及更改后的其他课程:
[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 NotificationId Notifications { get; set; }
[DataMember(Name = "CoverArt")]
public CoverArt Coverart { get; set; }
[DataMember(Name = "ProfileInfo")]
public 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 WaterMark WaterMark { get; set; }
}
[DataContract]
public class WaterMark
{
[DataMember(Name = "VideoWatermark")]
public VideoWatermark VideoWatermark { get; set; }
}
[DataContract]
public class VideoWatermark
{
[DataMember(Name = "WaterMarkInfo")]
public 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; }
}
您需要添加Newtonsoft.Json
的引用,然后使用
var wrapper = JsonConvert.DeserializeObject<Wrapper>(json);
RequestMetaDataXml RequestMetaDataXml = wrapper.RequestMetaDataXml;
如果您不想将List
更改为单个对象,则需要更新json并将List
类型对象值的值放在[ ]
括号中
e.g。
"Notifications": [{
"Id": "checktest1@ssss.com",
"Type": "ExtUser"
}],
等等。
但无论如何你还需要那个Wrapper
课程。
答案 2 :(得分:0)
var ser = new XmlSerializer(typeof(RequestMetaDataXml));
var settings = new XmlSerializerNamespaces();
settings.Add("", "");
using (var sw = new StringWriter())
{
ser.Serialize(sw, o.RequestMetaDataXml, settings);
var t = sw.ToString();
}