我正在将 JSON 有效负载传递给 API 控制器,其中一个字段是动态的,因为该字段需要作为 JSON 字符串再次传递给另一个 API。dotnet core 3.1 中间层不应该关心打字,因为有效载荷会改变。
这是传递到 API 控制器的对象:
public class GitHubAction
{
[JsonProperty("Title")]
public string Title { get; set; }
[JsonProperty("Enabled")]
[JsonConverter(typeof(BooleanParseStringConverter))]
public bool Enabled { get; set; }
[JsonProperty("Action")]
[JsonConverter(typeof(ExpandoObjectConverter))]
public dynamic Action { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是该dynamic对象在 VSCode 中的图片:
当我使用 JsonConvert.SerializeObject(x.Action);字符串时,结果没有被正确转换,而是序列化为 ValueKind: "{\"ValueKind\":1}"。
我想要得到的是作为 JSON 字符串的动作对象值,它应该看起来像 "{"createRepository":{"onboarding":{"service":{"organization":"foo","repositoryName":"foo-2-service","description":"A test service."}}}}"
是否有用于序列化动态对象的简单解决方案?