0

I have student class with array StudentPhones. When i remove StudentPhones property from Student class is working perfectly when post with Postman. But when add StudentPhones property then Postman gives me this error:

{ "StudentPhones": [ "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'DataAccess.StudentPhone' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath 'StudentPhones', line 4, position 23." ] }

 public class Student
 {
 public string StudentName { get; set; }
 public string StudentSurname { get; set; }
 public StudentPhone StudentPhones { get; set; }
 }
 public class StudentPhone
 {
 public int PhoneType{ get; set; }
 public string PhoneNumber { get; set; }
 }
 public async Task<ServiceResult> SaveStudent([FromBody]Student student)
 {
 }

How can I post this json? (I am using angular 6 in real, postman is only for example.)

my json

{
 "studentName": "test",
 "studentSurname": "test",
 "studentPhones": [
 {
 "phoneType": 1,
 "phoneNumber ": "111111111"
 },
 {
 "phoneType": 2,
 "phoneNumber ": "2222222222"
 }
 ],
}
asked Oct 1, 2018 at 10:55
4
  • 2
    Why don't you have StudentPhones declared as an array, or List, of StudentPhone in the Student class? Commented Oct 1, 2018 at 10:59
  • It was careless of me :) . Thanks for your reply. I am so ashamed for this question Commented Oct 1, 2018 at 11:04
  • That's okay! If you really want, you can delete the post. It will likely be closed and removed at some point because it was a simple mistake in the code. Commented Oct 1, 2018 at 11:05
  • I try delete the question but stackoverflow deny. Commented Oct 1, 2018 at 11:07

1 Answer 1

3

You have an array of phones in your JSON, so instead of public StudentPhone StudentPhones { get; set; } in your model, you should have public List<StudentPhone> StudentPhones { get; set; }

answered Oct 1, 2018 at 10:59

3 Comments

You should use a list of StudentPhones public List<StudentPhone> StudentPhones { get; set; }
It was careless of me :) . Thanks for your reply. I am so ashamed for this question
@HasanOzdemir Happens to all of us sometimes. I'll vote your question away.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.