How do I complete information that is in a complex JSON type?

Asked

Viewed 46 times

0

I am trying to carry out the filling of a Icollection type that is owned by my Professional model.

When performing the request I pass the information via JSON

request

But in VS debug and GET the information is not there!

VS

Get

  • 1

    Friend tries to change the property "Graduation" to "Graduations" equal is in his class, it will only popular if the names are identical.

2 answers

0

The name of your properties json may be equal to the object you are receiving as already quoted(I find this particularly horrible, rs). I will show a more "elegant" way to resolve, using the Newtonsoft.Json.

First install it in your project:

using Newtonsoft.Json;

Then on your object

public class RootObject
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("lastname")]
    public string Lastname { get; set; }

    [JsonProperty("cpf")]
    public string Cpf { get; set; }

    [JsonProperty("birthDate ")]
    public string BirthDate { get; set; }

    [JsonProperty("motherName")]
    public string MotherName { get; set; }

    [JsonProperty("postalCode")]
    public string PostalCode { get; set; }

    [JsonProperty("adress")]
    public string Adress { get; set; }

    [JsonProperty("adressNumber")]
    public string AdressNumber { get; set; }

    [JsonProperty("complement")]
    public string Complement { get; set; }

    [JsonProperty("background")]
    public string Background { get; set; }

    [JsonProperty("graduations")]
    public IEnumerable<Graduation> Graduations { get; set; }
}

public class Graduation
{
    [JsonProperty("instituionName")]
    public string InstituionName { get; set; }

    [JsonProperty("grade")]
    public string Grade { get; set; }

    [JsonProperty("formationYear")]
    public string FormationYear { get; set; }
}

Your Controller

[HttpPost]
public IActionResult Create([FromBody] RootObject obj)
{
   // Faz algo aqui 
   return Ok();
}

And you’ll finally get the long-awaited result, hehe.

-1


Do as Maycon said, your object has to have the same name with the same property with the identical names that will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.