简体   繁体   English

如何在 post 方法中反序列化 asp.net 核心 api 中的 json 数组?

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

how i can solve this problem (serialized/ and deserialize JSON array and store and read from sql server)?我该如何解决这个问题(序列化/反序列化 JSON 数组并从 sql 服务器存储和读取)? 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): 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" }

when i use postman for test I encounter this error:当我使用 postman 进行测试时,我遇到了这个错误:

postman error postman 错误

you can see my model, dto and action post in controller:您可以在 controller 中看到我的 model、dto 和操作帖子:

Model: Model:

  public class Recepie
    {
    [Key]
    public int ID { get; set; }
    [Required]
    public string RecepieName { get; set; }
    [Required]
    public string Ingredient { get; set; }   // include ingredientName / value / measure array
    [Required]
    public int CookingTime { get; set; }
    [Required]
    public string Level { get; set; }
    [Required]
    public int ServingPerson { get; set; }
    [Required]
    public string RecepieAbstration { get; set; }
    [Required]
    public string RecepieText { get; set; }
    [Required]
    public string NutritionalInformation { get; set; }
    [Required]
    public string Tags { get; set; }
  
    public DateTime CreateDate { get; set; }

    public DateTime ModifiedDate { get; set; }
    }

DTO: RecepieCreateDto DTO:RecepieCreateDto

    public class RecepieCreateDto
{

    public string RecepieName { get; set; }

    //public int MentorID { get; set; }

    public string Ingredient { get; set; }


    public int CookingTime { get; set; }

    public string Level { get; set; }

    public int ServingPerson { get; set; }

    public string RecepieAbstration { get; set; }

    public string RecepieText { get; set; }

    public string Tags { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime ModifiedDate { get; set; }
}

Action Post:行动帖:

        //Post api/recepie
    [HttpPost]
    public ActionResult <RecepieReadDto> CreateRecepie(RecepieCreateDto recepieCreateDto)
    {
        var recepieModel = _mapper.Map<Recepie>(recepieCreateDto);
        _repository.CreateRecepieObject(recepieModel);
        _repository.SaveChange();
        var recepieReadDto = _mapper.Map<RecepieReadDto>(recepieModel);
        return CreatedAtRoute(nameof(GetRecepieByID), new { Id = recepieReadDto.ID }, recepieReadDto);
        // return Ok(recepieModel);
    }

You are passing array for Ingredient but expecting string .您正在为成分传递数组,但期望字符串 Please change Ingredient to be List<Ingredient> where Ingredient is your new object which has name, value and messure.请将成分更改为List<Ingredient> ,其中成分是您的新 object,它具有名称、值和名称。 For example:例如:

 public class Ingredient { [Key] int Id {get; set; } string IngredientName { get; set; } string Value { get; set; } string Measure { get; set; } }

UPDATE: please update the DTO model respectively and use it in RecepieCreateDto更新:请分别更新 DTO model 并在RecepieCreateDto中使用

 public class IngredientDTO
 {
 string IngredientName { get; set; }
 string Value { get; set; }
 string Measure { get; set; }
 }

in RecepieCreateDto class Ingredient is a string but in your request json Ingredient is a array.在 RecepieCreateDto class 成分是一个字符串,但在您的请求 json 成分是一个数组。 so change RecepieCreateDto class or request json.所以将 RecepieCreateD 更改为 class 或请求 json。

"[{\"ingredientName\": \"43243\", \"value\": \"33\", \"measure\": \"\" },{ \"ingredientName\": \"565\" , \"value\": \"3\", \"measure\": \"\" }]"

OR或者

    public IList<Ingredient> Ingredient { get; set; }

You need to create a class for Ingredient property.您需要为成分属性创建一个 class。

public class Ingredient {
    public string IngredientName { get; set; }
    public string Value { get; set; } // This might be an int, you can change this on your purpose.
    public string Measure { get; set; } // This might be an int, you can change this on your purpose.
}

Looks like the ingredient you posting is a collection type so making Ingredient List<> or any other collection type might do it.看起来您发布的成分是一个集合类型,因此制作成分列表<> 或任何其他集合类型都可以做到这一点。

public class RecepieCreateDto
{

    public string RecepieName { get; set; }

    //public int MentorID { get; set; }

    public List<Ingredient> Ingredient { get; set; }


    public int CookingTime { get; set; }

    public string Level { get; set; }

    public int ServingPerson { get; set; }

    public string RecepieAbstration { get; set; }

    public string RecepieText { get; set; }

    public string Tags { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime ModifiedDate { get; set; }
}

You might need to make similar changes to your Recepie model after these changes.在这些更改之后,您可能需要对您的 Recepie model 进行类似的更改。

you can implement solution like below code ;
1 : Create View model For input Api;

  

 public class RecepieCreateViewModel
        {
    
            public string RecepieName { get; set; }
    
            public List<Ingredient> Ingredient { get; set; }
    
            public int CookingTime { get; set; }
    
            public string Level { get; set; }
    
            public int ServingPerson { get; set; }
    
            public string RecepieAbstration { get; set; }
    
            public string RecepieText { get; set; }
    
            public string NutritionalInformation { get; set; }
    
            public string Tags { get; set; }
    
            public DateTime CreateDate { get; set; }
    
            public DateTime ModifiedDate { get; set; }
        }

   

 public class Ingredient
    {
        public string ingredientName { get; set; }
        public string value { get; set; }
        public string measure { get; set; }
    }


2 : Create Dto Model

public class RecepieCreateDto
{

    public string RecepieName { get; set; }

    public string Ingredient { get; set; }

    public int CookingTime { get; set; }

    public string Level { get; set; }

    public int ServingPerson { get; set; }

    public string RecepieAbstration { get; set; }

    public string RecepieText { get; set; }

    public string NutritionalInformation { get; set; }

    public string Tags { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime ModifiedDate { get; set; }

}

3 : Cast Ingredient Model ToJson Object In Api And Create New RecepieCreateDto Like Below Code

  var result=Newtonsoft.Json.JsonConvert.SerializeObject(recepieCreateDto.Ingredient);
        

   RecepieCreateDto readDto = new RecepieCreateDto
    {
        CookingTime = recepieCreateDto.CookingTime,
        Level = recepieCreateDto.Level ,
        ModifiedDate = recepieCreateDto.ModifiedDate ,
        NutritionalInformation = recepieCreateDto.NutritionalInformation ,
        RecepieAbstration = recepieCreateDto.RecepieAbstration ,
        RecepieName = recepieCreateDto.RecepieName ,
        RecepieText = recepieCreateDto.RecepieText ,
        ServingPerson = recepieCreateDto.ServingPerson ,
        CreateDate = recepieCreateDto.CreateDate ,
        Tags = recepieCreateDto.Tags ,
        Ingredient=result,

    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM