2016-04-15 85 views
1

我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> 

回答

0

使生活更輕鬆,並使用圖書館做deserialisation你。一個常見的建議和我用的是Newtonsoft.Json,deserialisation是因爲這很容易:

JsonConvert.DeserializeObject<RequestMetaDataXml>(jsonStringReceived); 
+0

我試過這也沒有奏效。 – selvakumar

0

您對修改的一件事,JSON或DTO類原因是JSON WaterMarkVideoWatermark等單個對象而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類型對象值的值放入[ ]括號中

"Notifications": [{ 
     "Id": "[email protected]", 
     "Type": "ExtUser" 
}], 

等等。

但是無論如何你需要這個Wrapper類。

+0

但是我的通知可能會出現在 > 'Now only for ID和類型值不分配給DTO我是建築物通知作爲不同的對象 – selvakumar

+0

您的json是固定結構還是動態的? –

+0

我手動構建的那個通知部分就是Dynamic One – selvakumar

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(); 
     } 
相關問題