0

I have a code like this:

public IHttpActionResult Post(CUSTOMER [] newCustomer)
{
    try
    {
        foreach (CUSTOMER item in newCustomer)
        {
            var AddedCust = db.CUSTOMERs.Add(item);
        }

        int insertedRecords = db.SaveChanges();
        return Ok(insertedRecords);
    }
    catch (Exception)
    {
        return BadRequest(ModelState);
    }
}

With Postman I am trying to post more than one record - but I get an exception:

{

    "NAME": "test1",
    "SURNAME": "Aleksandrova",
    "BIRTHPLACE": "Minsk",
    "GENDER": "F",
    "IDENTITYNO": "AA75 857445",
    "IDENTITYPINCODE": 2552,
    "BIRTHDATE": "1970-06-19T00:00:00"
}
{

    "NAME": "test2",
    "SURNAME": "Aleksat34t34ndrova",
    "BIRTHPLACE": "Minsk",
    "GENDER": "F",
    "IDENTITYNO": "AA75 857445",
    "IDENTITYPINCODE": 2552,
    "BIRTHDATE": "1970-06-19T00:00:00"
}
{

    "NAME": "test3",
    "SURNAME": "Aleksandrova",
    "BIRTHPLACE": "Minsk",
    "GENDER": "F",
    "IDENTITYNO": "AA75 857445",
    "IDENTITYPINCODE": 2552,
    "BIRTHDATE": "1970-06-19T00:00:00"
}

ERROR:

"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'First_API.Models.CUSTOMER[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object.

How can I solve this problem?

2
  • 1
    Please read How to Ask. This error has been asked about - and answered hundreds of times already. What have you tried? Commented Feb 10, 2020 at 9:51
  • @CodeCaster I couldnt find the answer thats why I asked it Commented Feb 10, 2020 at 10:00

2 Answers 2

6

Your json body is invalid, Json array should be enclosed in [ ] (square brackets) and each record should be separated by ,(Comma)

Try to send api request with below json

[{

    "NAME": "test1",
    "SURNAME": "Aleksandrova",
    "BIRTHPLACE": "Minsk",
    "GENDER": "F",
    "IDENTITYNO": "AA75 857445",
    "IDENTITYPINCODE": 2552,
    "BIRTHDATE": "1970-06-19T00:00:00"
},
{

    "NAME": "test2",
    "SURNAME": "Aleksat34t34ndrova",
    "BIRTHPLACE": "Minsk",
    "GENDER": "F",
    "IDENTITYNO": "AA75 857445",
    "IDENTITYPINCODE": 2552,
    "BIRTHDATE": "1970-06-19T00:00:00"
},
{

    "NAME": "test3",
    "SURNAME": "Aleksandrova",
    "BIRTHPLACE": "Minsk",
    "GENDER": "F",
    "IDENTITYNO": "AA75 857445",
    "IDENTITYPINCODE": 2552,
    "BIRTHDATE": "1970-06-19T00:00:00"
}]
Sign up to request clarification or add additional context in comments.

3 Comments

how can I return just data itself which was added?
I know. I get the number of inserted records for example with one record I write return ok(value) but this value is in the loop How can i return it?
@AlishSafarli, error which you posted is self explanatory, it it addressed before on stackoverflow that might be the reason for getting -1 on your question
0

try

[
    {
        "NAME": "test1",
        "SURNAME": "Aleksandrova",
        "BIRTHPLACE": "Minsk",
        "GENDER": "F",
        "IDENTITYNO": "AA75 857445",
        "IDENTITYPINCODE": 2552,
        "BIRTHDATE": "1970-06-19T00:00:00"
    },
    {
        "NAME": "test2",
        "SURNAME": "Aleksat34t34ndrova",
        "BIRTHPLACE": "Minsk",
        "GENDER": "F",
        "IDENTITYNO": "AA75 857445",
        "IDENTITYPINCODE": 2552,
        "BIRTHDATE": "1970-06-19T00:00:00"
    },
    {
        "NAME": "test3",
        "SURNAME": "Aleksandrova",
        "BIRTHPLACE": "Minsk",
        "GENDER": "F",
        "IDENTITYNO": "AA75 857445",
        "IDENTITYPINCODE": 2552,
        "BIRTHDATE": "1970-06-19T00:00:00"
    }
]

3 Comments

You could add an explanation as to why you've formatted the JSON like you have, for completeness (see Prassad's answer).
"try" is not an answer.
@CodeCaster you can always do better

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.