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; }
}
2
  • What have you investigated? See this SO question for example. Commented Oct 31, 2018 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 Commented Oct 31, 2018 at 20:47

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Try a list of RootObject in your action: public string addWwarehouse([FromBody] List<RootObject> warehouses)
Yes sir it is working but can you explain how as we dont have anything like RootObject in our json data ?
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: 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.
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"
    }
}]

1 Comment

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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.