Skip to main content

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

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

 


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

 


Added NewtonsoftJson from Nuget and when running following error comes.

 


Hi @bhagyat25 ,

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


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