我有一个具有以下结构/类的控制器:
// model
public class Result
{
public string Document { get; set; } = "";
public int[,] Segments { get; set; } = new int[,] { };
}
// controller
public class SearchController : ControllerBase {
[HttpGet]
[Route("/api/v1/[controller]")]
[Produces("application/json")]
public IActionResult Search(//metadata list)
{
try {
Result result = <service-call-returning Result object>;
return Ok(result);
} catch (Exception e) {
return BadRequest("bad");
}
}
}
似乎它无法序列化 Result 对象,我收到以下异常:
失败:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
执行请求时发生未处理的异常。
System.NotSupportedException:不支持类型“System.Int32[,]”。
在 System.Text.Json.ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(Type propertyType)
在 System.Text.Json.Serialization.Converters.IEnumerableConverterFactory.CreateConverter(Type typeToConvert, JsonSerializerOptions 选项)
在 System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(Type typeToConvert , JsonSerializerOptions 选项)
在 System.Text.Json.JsonSerializerOptions.GetConverter(类型 typeToConvert)
如何使用(字符串,多维数组)序列化对象?另外我有一个期望结果的反应应用程序,字符串也将被序列化(有 \n\n \r ....),是客户端应用程序反序列化它的工作还是我需要找到一种返回非序列化 JSON 对象的方法?