ehsan khosravi

ehsan khosravi

  • NA
  • 20
  • 3.3k

how to deserialize json array in asp.net core api in post method?

Jan 6 2021 6:41 PM
how i can solve this problem (serialized/ and deserialize JSON array and store and read from sql server)? thank you
 
i created asp.net core api that connected to angular project and occurs problem when i want post json that contains json array(ingredient) :
 
{ "RecepieName": "3222",
"Ingredient": [ { "ingredientName": "43243", "value": "33", "measure": "" },
{ "ingredientName": "565" , "value": "3", "measure": "" }],
"CookingTime": 3,
"Level": "advance",
"ServingPerson": 3,
"RecepieAbstration": "good food",
"RecepieText": "",
"CreateDate": "2021-01-04T12:37:51.948",
"ModifiedDate": "2021-01-04T12:37:51.948" }
 
you can see my model , dto and action post in controller and DbContext :
 
 
Model :
  1. public class Recepie    
  2. {    
  3.     [Key]    
  4.     public int ID { getset; }    
  5.     [Required]    
  6.     public string RecepieName { getset; }    
  7.     [Required]    
  8.     public string Ingredient { getset; }   // include ingredientName / value / measure array    
  9.     [Required]    
  10.     public int CookingTime { getset; }    
  11.     [Required]    
  12.     public string Level { getset; }    
  13.     [Required]    
  14.     public int ServingPerson { getset; }    
  15.     [Required]    
  16.     public string RecepieAbstration { getset; }    
  17.     [Required]    
  18.     public string RecepieText { getset; }    
  19.     [Required]    
  20.     public string NutritionalInformation { getset; }    
  21.     [Required]    
  22.     public string Tags { getset; }        
  23.     public DateTime CreateDate { getset; }      
  24.     public DateTime ModifiedDate { getset; }    
  25. }   
DTO : RecepieCreateDto
  1. public class RecepieCreateDto  
  2. public string RecepieName { getset; }  
  3. //public int MentorID { get; set; }  
  4. public string Ingredient { getset; }  
  5. public int CookingTime { getset; }  
  6. public string Level { getset; }  
  7. public int ServingPerson { getset; }  
  8. public string RecepieAbstration { getset; }  
  9. public string RecepieText { getset; }  
  10. public string Tags { getset; }  
  11. public DateTime CreateDate { getset; }  
  12. public DateTime ModifiedDate { getset; }  
Action Post :
  1. //Post api/recepie    
  2. [HttpPost]    
  3. public ActionResult <RecepieReadDto> CreateRecepie(RecepieCreateDto recepieCreateDto)    
  4. {    
  5.     var recepieModel = _mapper.Map<Recepie>(recepieCreateDto);    
  6.     _repository.CreateRecepieObject(recepieModel);    
  7.     _repository.SaveChange();    
  8.     var recepieReadDto = _mapper.Map<RecepieReadDto>(recepieModel);    
  9.     return CreatedAtRoute(nameof(GetRecepieByID), new { Id = recepieReadDto.ID }, recepieReadDto);    
  10.     // return Ok(recepieModel);    
  11. }  
DbContext :
  1. public class FoodpackContext : DbContext  
  2. {  
  3. public FoodpackContext(DbContextOptions<FoodpackContext> opt):base(opt)  
  4. {  
  5. }  
  6. public DbSet<Recepie> Recepies { getset; }  

Answers (5)