0

I have below C# code for WEB API When I post below JSON the Account object is always null. Please, anyone can guide on below code.

public class Account
{
    public string id { get; set; }
    public string number { get; set; }
}

public object Post([FromBody] Account account)
{
    Message msg = new Message();
    msg.status = "Status";
    return Request.CreateResponse(msg);
}

JSON

{
  "Accounts": {
    "id": "13456454",
    "number": "222",
  }
}
3
  • You should post just { "id": "13456454", "number": "222", } Commented Jun 6, 2017 at 8:43
  • Account object is null, because in JSON you specify Accounts field, and it doesn't exist in C# model. You should either use { "id": "13456454", "number": "222", } as data you are POSTing or add extra model, that will contain that simpler model, with field named Accounts and type Account (not array of accounts, as name might suggest, because your JSON doesn't contain an array. Commented Jun 6, 2017 at 8:44
  • you you want get Account List Commented Jun 6, 2017 at 8:46

4 Answers 4

3

Your json:

{
  "Accounts": {
    "id": "13456454",
    "number": "222",
  }
}

It can be parsed to object with property Accounts which has id and number properties

If you want to post only one Account your json should be:

{
    "id": "13456454",
    "number": "222"
}
Sign up to request clarification or add additional context in comments.

Comments

1

You only need to post id and number in following format:

 {
     "id": "13456454",
     "number": "222",  
 }

This will wok for you.

Comments

1

Your Account object is null, because the C# code is inconsistent with JSON you provided. That's how MVC defends itself from badly formed requests.

Model suggests, that what you get in JSON are just these 2 pure fields, id and number, so JSON for it should look like this:

{
    "id": "13456454",
    "number": "222"
}

To make C# model adapted for provided JSON it should be built like this:

public class Accounts
{
    public Account Accounts { get; set; }
}

public class Account
{
    public string id { get; set; }
    public string number { get; set; }
}

And API Action prototype should look like this:

public object Post([FromBody] Accounts account)

By this you contain Account into another class with property Accounts resembling what you have in provided JSON.

However your naming scheme suggest that single request might contain more than single account. To achieve this C# Classes should look like this:

public class Accounts
{
    public Account[] Accounts { get; set; }
}

public class Account
{
    public string id { get; set; }
    public string number { get; set; }
}

And your JSON should contain object array:

{
    "Accounts": [
        {
            "id": "13456454",
            "number": "222",
        },
        {
            "id": "23456",
            "number": "125",
        },
    ]
}

Of course that array can contain only single element, if you need so, but it will allow you to put more than one Account at once, as your naming suggests.

For future reference, you should be more precise at what you want to be the result, to make sure we know best how we should help you.

2 Comments

Hi Buddy, I think is that a good syntax; public Account Accounts { get; set; }
Well, that's what I wrote.
0

You are sending list of accounts from the client side but api is expecting a single object if you want to sent single object you can send single object from json file else you can change server object as List

Comments

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.