Question: Mounting an JSON answer in WebAPI project

Question

Mounting an JSON answer in WebAPI project

Answers 1
Added at 2017-11-30 18:11
Tags
Question

In my project I'm developing a WebService (using WebAPI 2 with EF6). This service should return a JSON message and it is. But the message is an inconvenient format.

The controller is:

public class Arguments
{
    public int arg1{ get; set; }
    public int arg2{ get; set; }
}

[Route("webroute")]
[HttpPost]
[AcceptVerbs("POST")]
public IHttpActionResult FindTodos(Arguments args)
{
    return Ok(_repo.BuscaOrdens(args.arg1, args.arg2));
}

The repo method is as:

public object FindTodos(int arg1, int arg2)
    {
        dynamic nextDeadlines = _ctx.Todos
            .Where(o => o.field1 == arg1 && o.field2 == arg2)
            .OrderByDescending(o => o.field3)
            .ThenBy(o => o.ts)
            .Take(10)
            .Select(x => new
            {
                x.Field4,
                x.Field5
            }).ToList();

        dynamic delayed = _ctx.Todos
            .Where(o => o.field1 == arg1 && o.field2 == arg2)
            .OrderBy(o => o.field3)
            .ThenByDescending(o => o.ts)
            .Take(10)
            .Select(x => new
            {
                x.Field4,
                x.Field5
            }).ToList();

        return new List<object> { new List<object> {"NextDeadlines", nextDeadlines}, new List<object> { "Delayed", delayed} };
    }

The JSON return it is giving me is:

[
    [
        "NextDeadlines",
        [
            {
                "Field4": 2000,
                "Field5": 1.05
            },
            {
                "Field4": 1000,
                "Field5": 1
            }
        ]
    ],
    [
        "Delayed",
        [
            {
                "Field4": 500,
                "Field5": 1.1111111111111112
            }
        ]
    ]
]

And finally what I was expecting to get is something like this:

[
    "NextDeadlines":
    [
        {
            "Field4": 2000,
            "Field5": 1.05
        },
        {
            "Field4": 1000,
            "Field5": 1
        }
    ],
    "Delayed":
    [
        {
            "Field4": 500,
            "Field5": 1.1111111111111112
        }
    ]
]

Any ideas on what to correct??

Thanks in advance! Erich

Answers to

Mounting an JSON answer in WebAPI project

nr: #1 dodano: 2017-11-30 20:11

To begin, the json you are expecting is not valid. I suspect you meant something like

[
  {
    "NextDeadlines":
    [
        {
            "Field4": 2000,
            "Field5": 1.05
        },
        {
            "Field4": 1000,
            "Field5": 1
        }
    ],
    "Delayed":
    [
        {
            "Field4": 500,
            "Field5": 1.1111111111111112
        }
    ]
  }
]

If that's the case then you need to modify the final line to

    return new List<object> { new List<object> {"NextDeadlines", nextDeadlines}, new List<object> { "Delayed", delayed} };

to

    return new List<object> { new {NextDeadlines = nextDeadlines}, new { Delayed = delayed} };
Source Show
◀ Wstecz