Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Converting a dynamic Variable to List<model> or array

I want to send this Array with Ajax to api :

var receipt = Array();

        receipt = [
            { 'Service': 'Saab', 'Order': '20' },
            { 'Service': 'Volvo', 'Order': '12' },
            { 'Service': 'BMW', 'Order': '25' }
        ];

And this is my Ajax body :

var r = JSON.stringify(receipt);
        
        $.ajax({
            type: "POST",
            dataType: "Json",
            contentType: "application/json; charset=utf-8",
            data: r,
            url: "api/Receipt",
            success: function (result) {
                // To Do
                console.info(result);
            },
            error: function (result) {
                console.info(result);
            }
        });

And this is what i get as Json :

        {[
            {
               "Service": "Saab",
               "Order": "20"
            },
            {
               "Service": "Volvo",
               "Order": "12"
             },
             {
               "Service": "BMW",
               "Order": "25"
              }
          ]}

And i catch them in api side Like this :

[Route("api/Receipt")]
    [WebMethod]
    public string Receipt_Turn(object obj)
    {
        dynamic dyn = JsonConvert.DeserializeObject(obj.ToString());

        if (dyn != null)
        {
            return dyn[1].ToString();
        }
        else
        {
            return "false";
        }

    }

And it works, the part return dyn[1].ToString() returns second record of my Array. But i want to convert this dynamic variable to a list or array of a model .

Here is my model :

[Serializable()]
    public class Receipt
    {
        public string Service { get; set; }
        public string Order { get; set; }
    }

I want to act with it like this :

string serv = receipt[1].Service;

How can i do it ?

Thanks...

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Delete the Array Part in Your JS Script and Write it Like this :

var receipt = [
            { 'Service': 'Saab', 'Order': '20' },
            { 'Service': 'Volvo', 'Order': '12' },
            { 'Service': 'BMW', 'Order': '25' }
        ];

in this shape you are sending an Object. in your code i guess you are sending an JArray So it Will make some Problems in Converting from JArray to another Type .

in your API Side , Edit Your Code As Below :

public string Receipt_Turn(object r)
    {

        //important Line Here
        List<Receipt> rr = JsonConvert.DeserializeObject<List<Receipt>>(r.ToString());

        if (rr != null)
        {
            return rr[1].ToString();
        }
        else
        {
            return "false";
        }

    }

as you can see, we get it Like an Object and then we will be able to Get Ready your DeserializeObject part Like i mentioned.

you most declare a List of your Model, and after part DeserializeObject add that Type You want to Convert it to.

here Cause we Declared List<Receipt> , we should convert our DeserializeObject to it, so we add <List<Receipt>> after DeserializeObject.

and Finally you can use it Like You wish .

string serv = rr[1].Service;
over 4 years ago · Santiago Trujillo Report

0

Assuming you are using .NET Core, it should be able to bind it automatically if you do something like this:

```
[Route("api/Receipt")]
[WebMethod]
public string Receipt_Turn(List<Receipt> receipts)
{

    if (receipts != null)
    {
        return "seomthing";
    }
    else
    {
        return "false";
    }
}
```
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!