1

I had successfully deserialized a simple json object but when I send an array of json object using a POST request to my controller, it fails. Here is my code:

[HttpPost]
public string addWwarehouse([FromBody] warehouse warehouses)
{
      System.Diagnostics.Debug.WriteLine(warehouses[0].name);
      return "success";
}

This is working json data:

{
    "warehouses":
    {
        "name":"WarehouseA",
        "location":"locationA"
    }
}

But when I use an array like this,

[{
    "warehouses":
    {
        "name":"WarehouseA",
        "location":"locationA"
    }
}]

It doesn't work. I also tried using List<warehouse> but still no luck. This is my warehouse class:

public class warehouse { 
    public string name  { get; set; }
    public string location { get; set; }
}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Mohsin Mushtaq
  • 119
  • 1
  • 6
  • What have you investigated? See [this](https://stackoverflow.com/questions/10007722/post-array-as-json-to-mvc-controller) SO question for example. – Jeroen Heier Oct 31 '18 at 20:38
  • thankx for helping but in this question the request is not converted into json object but in my case it is as i already used `JSON.stringify` – Mohsin Mushtaq Oct 31 '18 at 20:47

2 Answers2

2

Welcome to StackOverflow.

It doesn't because your model is not a List<warehouse>:

[{
    "warehouses":
    {
        "name":"WarehouseA",
        "location":"locationA"
    }
}]

It is an array of objects each with a property called "warehouses" that contains a singular object of type warehouse. You might want to send an array of warehouses instead:

[{
    "name":"WarehouseA",
    "location":"locationA"
}, {
    "name":"WarehouseB",
    "location":"locationB"
}]

And deserialize it to List<warehouse>

P.S. If you have no control over json format then the answer by @Tiago Ávila should do the trick.

Let's inspect the model:

[{                                // [...] - array of some outer objs
    "warehouses":                 // with single property
    {                
        "name":"WarehouseA",      // inner obj that resides inside outer obj
        "location":"locationA"
    }
}]
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • Sir I already tried this approach but the problem is my json request is created like the above array as mentioned starting with `[]` brackets so is there any approach i could do to deserialize it ? – Mohsin Mushtaq Oct 31 '18 at 20:38
1

Because the way that your json is, you need a class like this:

public class Warehouses
{
    public string name { get; set; }
    public string location { get; set; }
}

public class RootObject
{
    public Warehouses warehouses { get; set; }
}

Then, in your action shoud be like this:

public string addWwarehouse([FromBody] RootObject warehouses)

I think this will resolve.

Tiago Ávila
  • 2,737
  • 1
  • 31
  • 34
  • Try a list of RootObject in your action: public string addWwarehouse([FromBody] List warehouses) – Tiago Ávila Oct 31 '18 at 20:45
  • Yes sir it is working but can you explain how as we dont have anything like `RootObject` in our json data ? – Mohsin Mushtaq Oct 31 '18 at 20:51
  • RootObject is just the name of the class, you can change for anything that makes more sense for you. What I did was, I took your json and converted to a c# class using that site: http://json2csharp.com/ and after that I realized that your json is inside of [] brackets, what means that you need a list of your class in your action. – Tiago Ávila Oct 31 '18 at 21:03