필드 포함
기본적으로 필드는 직렬화되지 않습니다. 다음 예제와 같이 JsonSerializerOptions.IncludeFields 전역 설정 또는 JsonInclude 특성을 사용하여 직렬화 또는 역직렬화할 때의 필드를 포함합니다.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Fields
{
public class Forecast
{
public DateTime Date;
public int TemperatureC;
public string? Summary;
}
public class Forecast2
{
[JsonInclude]
public DateTime Date;
[JsonInclude]
public int TemperatureC;
[JsonInclude]
public string? Summary;
}
public class Program
{
public static void Main()
{
string json = """
{
"Date":"2020-09-06T11:31:01.923395",
"TemperatureC":-1,
"Summary":"Cold"
}
""";
Console.WriteLine($"Input JSON: {json}");
var options = new JsonSerializerOptions
{
IncludeFields = true,
};
Forecast forecast = JsonSerializer.Deserialize<Forecast>(json, options)!;
Console.WriteLine($"forecast.Date: {forecast.Date}");
Console.WriteLine($"forecast.TemperatureC: {forecast.TemperatureC}");
Console.WriteLine($"forecast.Summary: {forecast.Summary}");
string roundTrippedJson =
JsonSerializer.Serialize<Forecast>(forecast, options);
Console.WriteLine($"Output JSON: {roundTrippedJson}");
Forecast2 forecast2 = JsonSerializer.Deserialize<Forecast2>(json)!;
Console.WriteLine($"forecast2.Date: {forecast2.Date}");
Console.WriteLine($"forecast2.TemperatureC: {forecast2.TemperatureC}");
Console.WriteLine($"forecast2.Summary: {forecast2.Summary}");
roundTrippedJson = JsonSerializer.Serialize<Forecast2>(forecast2);
Console.WriteLine($"Output JSON: {roundTrippedJson}");
}
}
}
// Produces output like the following example:
//
//Input JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}
//forecast.Date: 9/6/2020 11:31:01 AM
//forecast.TemperatureC: -1
//forecast.Summary: Cold
//Output JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}
//forecast2.Date: 9/6/2020 11:31:01 AM
//forecast2.TemperatureC: -1
//forecast2.Summary: Cold
//Output JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}
Imports System.Text.Json
Imports System.Text.Json.Serialization
Namespace Fields
Public Class Forecast
Public [Date] As Date
Public TemperatureC As Integer
Public Summary As String
End Class
Public Class Forecast2
<JsonInclude>
Public [Date] As Date
<JsonInclude>
Public TemperatureC As Integer
<JsonInclude>
Public Summary As String
End Class
Public NotInheritable Class Program
Public Shared Sub Main()
Dim json As String = "{""Date"":""2020-09-06T11:31:01.923395"",""TemperatureC"":-1,""Summary"":""Cold""}"
Console.WriteLine($"Input JSON: {json}")
Dim options As New JsonSerializerOptions With {
.IncludeFields = True
}
Dim forecast1 As Forecast = JsonSerializer.Deserialize(Of Forecast)(json, options)
Console.WriteLine($"forecast.Date: {forecast1.[Date]}")
Console.WriteLine($"forecast.TemperatureC: {forecast1.TemperatureC}")
Console.WriteLine($"forecast.Summary: {forecast1.Summary}")
Dim roundTrippedJson As String = JsonSerializer.Serialize(forecast1, options)
Console.WriteLine($"Output JSON: {roundTrippedJson}")
Dim forecast21 As Forecast2 = JsonSerializer.Deserialize(Of Forecast2)(json)
Console.WriteLine($"forecast2.Date: {forecast21.[Date]}")
Console.WriteLine($"forecast2.TemperatureC: {forecast21.TemperatureC}")
Console.WriteLine($"forecast2.Summary: {forecast21.Summary}")
roundTrippedJson = JsonSerializer.Serialize(forecast21)
Console.WriteLine($"Output JSON: {roundTrippedJson}")
End Sub
End Class
End Namespace
' Produces output like the following example:
'
'Input JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}
'forecast.Date: 9/6/2020 11:31:01 AM
'forecast.TemperatureC: -1
'forecast.Summary: Cold
'Output JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}
'forecast2.Date: 9/6/2020 11:31:01 AM
'forecast2.TemperatureC: -1
'forecast2.Summary: Cold
'Output JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}
읽기 전용 필드를 무시하려면 JsonSerializerOptions.IgnoreReadOnlyFields 전역 설정을 사용합니다.
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET