0

I have a model with a List<string> Parameters {get; set;} property, so when I send from postman a json like this

{
    ...,
    "Parameters" :
    [
        ["FirstParam"],
        ["SecondParam"]
    ],
    ...
}

I receive null in my controller method, I mean the requestModel is null

public JsonResult GetBalances([FromBody]RequestModel requestModel)
{
    ...
}

I know that "Parameters" is the problem because when I send without it, I receive the model normally.

How can I receive a list of parameters(string) from a json format?

PD. It works when I create a model like

public class RequestModel
{
    ...
    public List<Parameters> Parameters {get;set;}
}

public class Parameters 
{ 
    public string Name {get;set;} 
}

And the json

{
    ...,
    "Parameters" : [
        {"Name": "FirstParameter"}, 
        {"Name":"SecondParameter"}
    ]
    ...
}

But I wonder if it is possible get it without create a class. Thanks in advance.

1 Answer 1

1

I found the problem, the json format was wrong. My final json:

{
    ...,
    "Parameters" :
    [
        "FirstParam",
        "SecondParam"
    ],
    ...
}

I think I was sending a list of arrays of strings (or something like that). Now I can use List<string> as a property in my model.

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

3 Comments

Both json are valid, the thing is one is an array and the other one is a array of arrays I think
Run the JSON through json2csharp.com ...it will show you the json represented as classes.
Great tool!, I got List<List<string>>, that was my error. Thanks

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.