Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ helpviewer_keywords:
- "deserialization"
ms.topic: concept-article
ms.custom: copilot-scenario-highlight
#customer intent: As a developer, I want to learn how to use System.Text.Json to deserialize JSON data.
---

# How to read JSON as .NET objects (deserialize)
Expand All @@ -28,10 +27,10 @@ Any JSON properties that aren't represented in your class are ignored [by defaul

## Examples

The following example shows how to deserialize a JSON string:
The following example shows how to deserialize a JSON string that contains collections and nested objects:

:::code language="csharp" source="snippets/how-to/csharp/DeserializeExtra.cs" highlight="54-55":::
:::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="Deserialize":::
:::code language="vb" source="snippets/how-to/vb/DeserializeExtra.vb":::

To deserialize from a file by using synchronous code, read the file into a string, as shown in the following example:

Expand Down Expand Up @@ -105,5 +104,6 @@ For more information about GitHub Copilot, see GitHub's [FAQs](https://github.co

## See also

- [How to deserialize to an interface](https://gist.github.com/tonysneed/5e7988516b081d454cde95b5d729e1af)
- [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
- [GitHub Copilot in Visual Studio Code](https://code.visualstudio.com/docs/copilot/overview)
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,44 @@ public static void Main()
Console.WriteLine($"Date: {weatherForecast?.Date}");
Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}");
Console.WriteLine($"Summary: {weatherForecast?.Summary}");

if (weatherForecast?.DatesAvailable != null)
{
foreach (DateTimeOffset dateTimeOffset in weatherForecast.DatesAvailable)
{
Console.WriteLine($"DateAvailable: {dateTimeOffset}");
}
}

if (weatherForecast?.TemperatureRanges != null)
{
foreach (KeyValuePair<string, HighLowTemps> temperatureRange in weatherForecast.TemperatureRanges)
{
Console.WriteLine($"TemperatureRange: {temperatureRange.Key} is {temperatureRange.Value.Low} to {temperatureRange.Value.High}");
}
}

if (weatherForecast?.SummaryWords != null)
{
foreach (string summaryWord in weatherForecast.SummaryWords)
{
Console.WriteLine($"SummaryWord: {summaryWord}");
}
}
}
}
}
// output:
//Date: 8/1/2019 12:00:00 AM -07:00
//TemperatureCelsius: 25
//Summary: Hot

/* Output:
*
* Date: 8/1/2019 12:00:00 AM -07:00
* TemperatureCelsius: 25
* Summary: Hot
* DateAvailable: 8/1/2019 12:00:00 AM -07:00
* DateAvailable: 8/2/2019 12:00:00 AM -07:00
* TemperatureRange: Cold is -10 to 20
* TemperatureRange: Hot is 20 to 60
* SummaryWord: Cool
* SummaryWord: Windy
* SummaryWord: Humid
* */
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static async Task Main(string[] args)
//Console.WriteLine("\n============================= Roundtrip enum as string\n");
//RoundtripEnumAsString.Run();

SerializeEnumCustomName.Run();
//SerializeEnumCustomName.Run();

//Console.WriteLine("\n============================= Roundtrip enum using JsonConverterAttribute\n");
//RoundtripEnumUsingConverterAttribute.Run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ public static void Main()
TemperatureCelsius = 25,
Summary = "Hot",
SummaryField = "Hot",
DatesAvailable = new List<DateTimeOffset>()
{ DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02") },
DatesAvailable = [DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02")],
TemperatureRanges = new Dictionary<string, HighLowTemps>
{
["Cold"] = new HighLowTemps { High = 20, Low = -10 },
["Hot"] = new HighLowTemps { High = 60 , Low = 20 }
},
SummaryWords = new[] { "Cool", "Windy", "Humid" }
SummaryWords = ["Cool", "Windy", "Humid"]
};

var options = new JsonSerializerOptions { WriteIndented = true };
Expand All @@ -46,7 +45,8 @@ public static void Main()
}
}
}
// output:

// Output:
//{
// "Date": "2019-08-01T00:00:00-07:00",
// "TemperatureCelsius": 25,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Imports System.Text.Json

Namespace DeserializeExtra

Public Class WeatherForecast
Public Property [Date] As DateTimeOffset
Public Property TemperatureCelsius As Integer
Public Property Summary As String
Public SummaryField As String
Public Property DatesAvailable As IList(Of DateTimeOffset)
Public Property TemperatureRanges As Dictionary(Of String, HighLowTemps)
Public Property SummaryWords As String()
End Class

Public Class HighLowTemps
Public Property High As Integer
Public Property Low As Integer
End Class

Public NotInheritable Class Program

Public Shared Sub Run()
Dim jsonString As String =
"{
""Date"": ""2019-08-01T00:00:00-07:00"",
""TemperatureCelsius"": 25,
""Summary"": ""Hot"",
""DatesAvailable"": [
""2019-08-01T00:00:00-07:00"",
""2019-08-02T00:00:00-07:00""
],
""TemperatureRanges"": {
""Cold"": {
""High"": 20,
""Low"": -10
},
""Hot"": {
""High"": 60,
""Low"": 20
}
},
""SummaryWords"": [
""Cool"",
""Windy"",
""Humid""
]
}"

Dim weatherForecast As WeatherForecast =
JsonSerializer.Deserialize(Of WeatherForecast)(jsonString)

Console.WriteLine($"Date: {weatherForecast?.Date}")
Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}")
Console.WriteLine($"Summary: {weatherForecast?.Summary}")

If weatherForecast?.DatesAvailable IsNot Nothing Then
For Each dateTimeOffset As DateTimeOffset In weatherForecast.DatesAvailable
Console.WriteLine($"DateAvailable: {dateTimeOffset}")
Next
End If

If weatherForecast?.TemperatureRanges IsNot Nothing Then
For Each temperatureRange As KeyValuePair(Of String, HighLowTemps) In weatherForecast.TemperatureRanges
Console.WriteLine($"TemperatureRange: {temperatureRange.Key} is {temperatureRange.Value.Low} to {temperatureRange.Value.High}")
Next
End If

If weatherForecast?.SummaryWords IsNot Nothing Then
For Each summaryWord As String In weatherForecast.SummaryWords
Console.WriteLine($"SummaryWord: {summaryWord}")
Next
End If
End Sub

End Class

End Namespace

' Output:
'
'Date: 8/1/2019 12:00:00 AM -07:00
'TemperatureCelsius: 25
'Summary: Hot
'DateAvailable: 8/1/2019 12:00:00 AM -07:00
'DateAvailable: 8/2/2019 12:00:00 AM -07:00
'TemperatureRange: Cold is -10 to 20
'TemperatureRange: Hot is 20 to 60
'SummaryWord: Cool
'SummaryWord: Windy
'SummaryWord: Humid