Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Deserialize in Dynamics 365 CRM - JSON string to .NET object

Posted on by 185

Hi,

I am trying to deserialize JSON string to .NET object in a plugin.I am using DataContractJsonSerializer as  JavaScriptSerializer has security issues. But , still I am facing below error.

Error Message : 

Type 'UpdateOpportunitiesFromIntegration.jsondataclass' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

Code :

string jsonobject = entity.Attributes["ae_operativeintegrationobject"].ToString();

using (System.IO.MemoryStream DeserializeStream = new System.IO.MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(jsondataclass));

System.IO.StreamWriter writer = new System.IO.StreamWriter(DeserializeStream);
writer.Write(jsonobject);
writer.Flush();
DeserializeStream.Position = 0;

jsondataclass deserializeobj = (jsondataclass)serializer.ReadObject(DeserializeStream);
}

Class file added to solution :

class jsondataclass
{
public string SalesOrderId { get; set; }
public string SalesOrderName { get; set; }
public string SalesOrderStage { get; set; }
public string OrderStartDate { get; set; }
public string OrderEndDate { get; set; }
public string TotalOrderValue { get; set; }
}

*This post is locked for comments

  • Dynamics 365 CRM Profile Picture
    Dynamics 365 CRM 185 on at
    RE: Deserialize in Dynamics 365 CRM - JSON string to .NET object

    Hi,

    I resolved the issue by updating the class with List. Actually, the json string has multiple values,so I need to use list.

    [DataContract]

       public class jsondataclass

       {

           [DataMember(Name = "SalesOrderId")]

           public string SalesOrderId { get; set; }

           [DataMember(Name = "SalesOrderName")]

           public string SalesOrderName { get; set; }

           [DataMember(Name = "SalesOrderStage")]

           public string SalesOrderStage { get; set; }

           [DataMember(Name = "OrderStartDate")]

           public string OrderStartDate { get; set; }

           [DataMember(Name = "OrderEndDate")]

           public string OrderEndDate { get; set; }

           [DataMember(Name = "TotalOrderValue")]

           public string TotalOrderValue { get; set; }

       }

       [CollectionDataContract]

       public class jsondataclasses : List<jsondataclass>

       {

       }

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Deserialize in Dynamics 365 CRM - JSON string to .NET object

    Hi,

    Could you share full code?

    try to avoid using as go by objects and pass memory stream in to write and see if it works

    another sample from micorosft docs - docs.microsoft.com/.../how-to-serialize-and-deserialize-json-data

  • Dynamics 365 CRM Profile Picture
    Dynamics 365 CRM 185 on at
    RE: Deserialize in Dynamics 365 CRM - JSON string to .NET object

    Thanks. As both of you suggested, I updated the class with "DataContract" and I am not facing any error now.But, the response I am getting null.

    Updated Class :

    [DataContract]

       public class jsondataclass

       {

           [DataMember(Name = "SalesOrderId")]

           public string SalesOrderId { get; set; }

           [DataMember(Name = "SalesOrderName")]

           public string SalesOrderName { get; set; }

           [DataMember(Name = "SalesOrderStage ")]

           public string SalesOrderStage { get; set; }

           [DataMember(Name = "OrderStartDate ")]

           public string OrderStartDate { get; set; }

           [DataMember(Name = "OrderEndDate ")]

           public string OrderEndDate { get; set; }

           [DataMember(Name = "TotalOrderValue ")]

           public string TotalOrderValue { get; set; }

       }

    Sample string I am passing to deserialize :

    [{"SalesOrderId":"9777","SalesOrderName":"MX HIGHER - KYT LAST - Fiscal 2016","SalesOrderStage":"Active Order (O1) - 100%","OrderStartDate":"2015-12-01","OrderEndDate":"2016-09-30","TotalOrderValue":"471941.7100"}]

  • Verified answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Deserialize in Dynamics 365 CRM - JSON string to .NET object

    Hi,

    we have to define the class as "DataContract" and members as "DataMembers". see example - www.magnetismsolutions.com/.../how-to-serialize-to-json-in-sandboxed-plugin-for-dynamics-365

  • Verified answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Deserialize in Dynamics 365 CRM - JSON string to .NET object

    Hi

    You will have to decorate your class with DatContract and your attribtues with DataMember

    [DataContract(Name = "jsondataclass", Namespace = "http://www.contoso.com")]

    class jsondataclass

    {

    [DataMember(Name = "SalesOrderId")]

    public string SalesOrderId { get; set; }

    [DataMember(Name = "SalesOrderName")]

    public string SalesOrderName { get; set; }

    [DataMember(Name = "SalesOrderStage ")]

    public string SalesOrderStage { get; set; }

    [DataMember(Name = "OrderStartDate ")]

    public string OrderStartDate { get; set; }

    [DataMember(Name = "OrderEndDate ")]

    public string OrderEndDate { get; set; }

    [DataMember(Name = "TotalOrderValue ")]

    public string TotalOrderValue { get; set; }

    }

    Have a look at the following link as well

    https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractattribute?view=netframework-4.7.2

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans