Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET Core 3 System.Text.Json嵌套对象序列化_.net_Json_Asp.net Core_.net Core_System.text.json - Fatal编程技术网

.NET Core 3 System.Text.Json嵌套对象序列化

.NET Core 3 System.Text.Json嵌套对象序列化,.net,json,asp.net-core,.net-core,system.text.json,.net,Json,Asp.net Core,.net Core,System.text.json,只需使用VS2019 web应用程序模板玩新功能: 天气预报等级声明如下: using System; namespace WebApplication4 { public class WeatherForecast { public DateTime Date { get; set; } public int TemperatureC { get; set; } public int TemperatureF => 32

只需使用VS2019 web应用程序模板玩新功能:

天气预报等级声明如下:

using System;

namespace WebApplication4
{
    public class WeatherForecast
    {
        public DateTime Date { get; set; }
        public int TemperatureC { get; set; }
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
        public string Summary { get; set; }
    }
}
示例方法:

 [HttpGet("Test1")]
    public WeatherForecast Test1()
    {
        WeatherForecast forecast = new WeatherForecast();
        return forecast;
    }
这工作正常,返回: {“日期”:“0001-01-01T00:00:00”,“温度”:0,“温度”:32,“摘要”:null}

但是这个代码:

 public class TestClass
    {
        public WeatherForecast Forecast;
    }

    [HttpGet("Test")]
    public TestClass Test()
    {
        WeatherForecast forecast = new WeatherForecast();
        TestClass test = new TestClass()
        {
            Forecast = forecast
        };
        return test;
    }
返回emply json对象:{}


如何序列化嵌套对象?

您需要使用属性,因为字段可能无法序列化。添加get并设置为forecast

public class TestClass
{
    public WeatherForecast Forecast {get;set;}
}

完全相同的问题。在一个模型上错过了接受者和接受者。他在拔我的头发。非常感谢。