Solved

Deserialize Json output

  • 3 October 2023
  • 5 replies
  • 97 views

Userlevel 3
Badge

I need to convert following Json output into List. Can someone help me?

[{"VendorCode":"V000000001","UTRNo":"T045353","IsValid":true,"VendorTypeID":1},{"VendorCode":"V000000002","UTRNo":"S569000","IsValid":true,"VendorTypeID":2},{"VendorCode":"V000000003","UTRNo":"S569000","IsValid":false,"VendorTypeID":2}]

This is my code.

protected virtual IEnumerable verifyVendor(PXAdapter adapter)
{            
            HMRCAPIConfigDetail hMRCAPIConfigDetail = SelectFrom<HMRCAPIConfigDetail>.Where<HMRCAPIConfigDetail.active.IsEqual<True>>.View.Select(this) ;

            if (hMRCAPIConfigDetail == null)
            {
                WebDialogResult result = VendorRegisterDetail.Ask(ActionsMessages.Warning, Messages.APIActiveErrorMessage,
                MessageButtons.OK, MessageIcon.Warning, true);
            }
            else
            {
                var client = new RestClient();                
                var request = new RestRequest(hMRCAPIConfigDetail.Apibaseurl, Method.GET);

                RestResponse response = (RestResponse)client.Execute(request);
                if (response == null)
                {
                    VendorRegisterDetail.Ask(ActionsMessages.Warning, "Integration Error. Please Contact System Admin",
                MessageButtons.OK, MessageIcon.Warning, true);
                }
                else
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        //Need to convert to List here
                    }
                    else
                    {
                        VendorRegisterDetail.Ask(ActionsMessages.Warning, response.ErrorMessage, MessageButtons.OK, MessageIcon.Warning, true);
                    }
                }
               Console.WriteLine(response.Content);
            }
            return adapter.Get();
}

icon

Best answer by abhijit 3 October 2023, 20:08

View original

5 replies

Badge

You need to define an equivalent structure of the JSON structure in c#.
For example, your structure could look like this:

 

// Root myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse);
public class Root
{
public string VendorCode { get; set; }
public string UTRNo { get; set; }
public bool IsValid { get; set; }
public int VendorTypeID { get; set; }
}

Then you’ll need a library like Newtonsoft JSON to covert the data into a list. The code will look something like this.
 

Root myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse);

 

Userlevel 7
Badge +4

@bhagyat25, Same as the suggestion by @abhijit.

As I just built an example, posting below

    public class SOOrderEntry_Extension : PXGraphExtension<PX.Objects.SO.SOOrderEntry>
{
#region Event Handlers
public PXAction<SOOrder> action;
[PXUIField(DisplayName = "Actions")]
[PXButton(SpecialType = PXSpecialButtonType.ActionsFolder)]
protected IEnumerable Action(PXAdapter adapter)
{
string jsonDept = @"[{""VendorCode"":""V000000001"",""UTRNo"":""T045353"",""IsValid"":true,""VendorTypeID"":1}]";
var serializer = new JavaScriptSerializer();
List<VendorData> deptObj = serializer.Deserialize<List<VendorData>>(jsonDept);
return adapter.Get();
}

public class VendorData
{
public string VendorCode { get; set; }
public string UTRNo { get; set; }
public bool IsValid { get; set; }
public int VendorTypeID { get; set; }
}
#endregion
}

 

Userlevel 3
Badge

Added NewtonsoftJson from Nuget and when running following error comes.

 

Userlevel 7
Badge +11

Hi @bhagyat25 ,

Can you add the Newtonsoft reference from the Acumatica instance folder/bin folder? 

Badge

Added NewtonsoftJson from Nuget and when running following error comes.

 

I suspect there is an existing Newtonsoft.Json being referenced. So you’re getting this message. Try to find that dll and reference it, instead of adding it from Nuget Packages.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved