简体   繁体   中英

How to deserialize Json String into Custom DTO C# object?

I have DTO Classes .I have Received Json serialised string like this how can i assign into the Above DTO? i am trying to deserialize the json string like that it is not working for me.

RequestMetaDataXml MetaDataDTO =new RequestMetaDataXml();
                MetaDataDTO = JsonConvert.DeserializeObject<RequestMetaDataXml>(arguments["MetaDataDTO"]);

i am generatig Json in the following method

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

How to deserialize the Values of Json in c# code and how to assign these values into my custom DTO?

Make life easier and use a library to do the deserialisation for you. One commonly recommended and which I use is Newtonsoft.Json, deserialisation is as easy as this:

JsonConvert.DeserializeObject<RequestMetaDataXml>(jsonStringReceived);

You've to modify one thing, either json or DTO class because json has a single object for WaterMark , VideoWatermark etc. while DTO class says these are List type objects. Also, you need a wrapper class because RequestMetaDataXml is wrapped in another object.

Wrapper class:

[DataContract]
public class Wrapper
{
    [DataMember(Name = "RequestMetaDataXml")]
    public RequestMetaDataXml RequestMetaDataXml { get; set; }
}

and your other classes after changes are:

[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; }
}

You've to add a reference of Newtonsoft.Json and then use

 var wrapper = JsonConvert.DeserializeObject<Wrapper>(json);
 RequestMetaDataXml RequestMetaDataXml = wrapper.RequestMetaDataXml;

If you don't want to change List to single objects, you've to update your json and put values for List type objects values in [ ] braces eg

"Notifications": [{
        "Id": "checktest1@ssss.com",
        "Type": "ExtUser"
 }],

and so on.

But you'll need that Wrapper class anyway.

        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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM