How to serialize a dynamic object to a JSON string in dotnet core?

后端 未结 2 766
广开言路
广开言路 2021-01-20 17:42

I am passing a JSON payload to an API Controller, and one of the fields is dynamic because the field needs to be passed again as a JSON string to another API. The dotnet co

相关标签:
2条回答
  • 2021-01-20 17:58

    I had the same problem and I fixed it this way

    using System.Text.Json;
    
    string serializedObject = JsonSerializer.Serialize(x.Action); //Instead of use JsonConvert.SerializeObject(x.Action);
    

    Instead of Newtonsoft.Json you have to use System.Text.Json.

    0 讨论(0)
  • 2021-01-20 18:12

    Try this

    var jsonString=s.Action.ToString();
    

    This will give you the json string

    0 讨论(0)
提交回复
热议问题