web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Verified answer
    Kokulan Profile Picture
    18,054 on at

    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

  • Verified answer
    Sreevalli Profile Picture
    3,264 on at

    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

  • Dynamics 365 CRM Profile Picture
    185 on at

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

  • Suggested answer
    Sreevalli Profile Picture
    3,264 on at

    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
    185 on at

    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>

       {

       }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans