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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Json string into X++ object

(0) ShareShare
ReportReport
Posted on by 535

Hi Experts,

I'm working on  AX 2009 integration with third party web application. 

I have consumed the json api and converted into C# object and added the (.dll) in ax 2009. I am getting the json value as string and unable to de-serialize the data in AX using x++.

Anyone help here, how to send the json data in list to AX or appreciate if any alternate suggestion ?

Tools:

1. VS 2010 Professional.

2. AX 2009 SP1.

Thanks in advance and Regards,

Vignesh

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    I don't understand your situation. You said you "have consumed the json api and converted into C# object". But then you claim you're "getting the json value as string" - so what happened with the C# object you just mentioned?

  • Vignesh R Profile Picture
    535 on at

    Hi Martin,

    Kindly refer the below points:

    In C#,

    1. I have consumed the Json api and returning the json data.

    In AX,

    1. Add the dll file in AX Bin folder.

    2. Added the reference of dll.

    3. Created a Job to consume the Json data and getting the value in string format.

    Question: How to deserialize the json data in ax?

  • Suggested answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    Aha, so you return a string and not an object with the data. That's what you can change - create a class the data, deserialize the JSON string to an object of this class and return this object to AX. In AX, use CLR Interop to read data from the object and do whatever you like with it.

    You can easily create a C# class to hold the data by using Edit > Paste Special > Paste JSON as Classes. It probably doesn't exist in VS2010, but you can use a newer version just to create the class(es) (unless it's very simple and it's easier to do it manually).

    To deserialize JSON to an object, you can use Newtonsoft.Json library.

  • Vignesh R Profile Picture
    535 on at

    Hi Martin,

    Thanks for your response.

    You mean to say like below code?

    using System;  

    using System.Collections.Generic;  

    using System.Linq;  

    using System.Text;  

    using System.IO;  

    using System.Runtime.Serialization.Json;  

    using System.Runtime.Serialization;  

    namespace Integration.JsonAdapter  

    {  

      public class Convert  

      {  

        // Deserialize a JSON stream to a User object.  

        public static Person ReadToObject(string json)  

        {  

          Person deserializedUser = new Person();  

          MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));  

          DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());  

          deserializedUser = ser.ReadObject(ms) as Person;  

          ms.Close();  

          return deserializedUser;  

        }  

      }  

      [DataContract]  

      public class Person  

      {  

        [DataMember]  

        internal string name;  

        [DataMember]  

        internal int age;  

      }  

    }  

  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    Yes, you can create a data contract class and utilize DataContractJsonSerializer if you prefer.

    The solution using Newtonsoft.Json would look like this:

    public static Person ReadToObject(string json)
    {
        return JsonConvert.DeserializeObject<Person>(json);
    }
  • Vignesh R Profile Picture
    535 on at

    If you have a time, please check the below code and correct me if wrong

    using System;

    using System.Collections.Generic;

    using System.IO;

    using System.Linq;

    using System.Net;

    using System.Text;

    using Newtonsoft.Json;

    using System.Runtime.Serialization;

    using System.Runtime.Serialization.Json;

    namespace Test_CustomerMasters

    {

       public class Class1

       {

           public string ConsumeJson()

           {

               Uri requestUri = new Uri("JsonApi");

               WebRequest request = WebRequest.Create(requestUri);

               request.Method = "GET";

               request.ContentType = @"application/json; charset=utf-8";

               var authHeader = "Tokenvalue";

               request.Headers.Add("x-access-token", authHeader);

               HttpWebResponse accountresponse = (HttpWebResponse)request.GetResponse();

               string accountjsonres = null;

               StreamReader sr12 = new StreamReader(accountresponse.GetResponseStream());

               {

                   accountjsonres = sr12.ReadToEnd();

               }

               return accountjsonres;          

           }

           public static CustomerList ReadToObject(string json)

           {

               return JsonConvert.DeserializeObject<CustomerList>(json);

           }

       }

       public class CustomerList

       {

           public string goC_Code { get; set; }

           public string customer_Name { get; set; }

           public string currency { get; set; }

           public object unit { get; set; }

           public string customer_Telephone { get; set; }

           public string customer_Fax { get; set; }

           public string customer_Country { get; set; }

           public string customer_State { get; set; }

           public string customer_Zip { get; set; }

           public string customer_city { get; set; }

           public object customer_Street { get; set; }

           public string customer_Mobile { get; set; }

           public string customerDepartment { get; set; }

           public string project { get; set; }

           public string employee { get; set; }

           public string purpose { get; set; }

           public object customerContact { get; set; }

           public string industryClassification { get; set; }

           public string companyURL { get; set; }

           public string customer_Type { get; set; }

           public string taxGroup { get; set; }

           public string payTerm { get; set; }

           public object bu { get; set; }

           public object sbu { get; set; }

           public string street { get; set; }

           public object city { get; set; }

           public object state { get; set; }

           public string zipCode { get; set; }

           public object country { get; set; }

           public int id { get; set; }

       }

    }

  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    One problem I see is that your code isn't releasing resources, such as that you're not closing the stream reader.

    In C#, you can easily do it with the 'using' statement (assuming that the objects implement IDisposable). Like this:

    using (HttpWebResponse accountResponse = (HttpWebResponse)request.GetResponse())
    using (Stream stream = accountResponse.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
       accountJsonRes = reader.ReadToEnd();
    }
  • Vignesh R Profile Picture
    535 on at

    Thanks martin.

    How will I get data in AX 2009 using CLR Interop from the object?

    Thanks in advance and Regards,

    Vignesh

  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    If you know how to call your method, you already know how to access CLR (.NET) classes via .NET Interop. So... what's problem you're unable to solve?

  • Vignesh R Profile Picture
    535 on at

    I am unable to call  "ReadToObject" method in AX.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans