Skip to content

Commit fb918d9

Browse files
Copilotgewarren
andcommitted
Add deserialization examples for child collections and nested objects
Co-authored-by: gewarren <[email protected]>
1 parent a3ce430 commit fb918d9

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

docs/standard/serialization/system-text-json/deserialization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ Any JSON properties that aren't represented in your class are ignored [by defaul
2828

2929
## Examples
3030

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

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

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

docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,41 @@ public static void Main()
5757
Console.WriteLine($"Date: {weatherForecast?.Date}");
5858
Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}");
5959
Console.WriteLine($"Summary: {weatherForecast?.Summary}");
60+
61+
if (weatherForecast?.DatesAvailable != null)
62+
{
63+
foreach (DateTimeOffset dateTimeOffset in weatherForecast.DatesAvailable)
64+
{
65+
Console.WriteLine($"DateAvailable: {dateTimeOffset}");
66+
}
67+
}
68+
69+
if (weatherForecast?.TemperatureRanges != null)
70+
{
71+
foreach (KeyValuePair<string, HighLowTemps> temperatureRange in weatherForecast.TemperatureRanges)
72+
{
73+
Console.WriteLine($"TemperatureRange: {temperatureRange.Key}, {temperatureRange.Value.Low} - {temperatureRange.Value.High}");
74+
}
75+
}
76+
77+
if (weatherForecast?.SummaryWords != null)
78+
{
79+
foreach (string summaryWord in weatherForecast.SummaryWords)
80+
{
81+
Console.WriteLine($"SummaryWord: {summaryWord}");
82+
}
83+
}
6084
}
6185
}
6286
}
6387
// output:
6488
//Date: 8/1/2019 12:00:00 AM -07:00
6589
//TemperatureCelsius: 25
6690
//Summary: Hot
91+
//DateAvailable: 8/1/2019 12:00:00 AM -07:00
92+
//DateAvailable: 8/2/2019 12:00:00 AM -07:00
93+
//TemperatureRange: Cold, -10 - 20
94+
//TemperatureRange: Hot, 20 - 60
95+
//SummaryWord: Cool
96+
//SummaryWord: Windy
97+
//SummaryWord: Humid
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Imports System.Text.Json
2+
3+
Namespace SystemTextJsonSamples
4+
5+
Public NotInheritable Class DeserializeExtra
6+
7+
Public Shared Sub Run()
8+
Dim jsonString As String =
9+
"{
10+
""Date"": ""2019-08-01T00:00:00-07:00"",
11+
""TemperatureCelsius"": 25,
12+
""Summary"": ""Hot"",
13+
""DatesAvailable"": [
14+
""2019-08-01T00:00:00-07:00"",
15+
""2019-08-02T00:00:00-07:00""
16+
],
17+
""TemperatureRanges"": {
18+
""Cold"": {
19+
""High"": 20,
20+
""Low"": -10
21+
},
22+
""Hot"": {
23+
""High"": 60,
24+
""Low"": 20
25+
}
26+
},
27+
""SummaryWords"": [
28+
""Cool"",
29+
""Windy"",
30+
""Humid""
31+
]
32+
}"
33+
34+
Dim weatherForecast As WeatherForecastWithPOCOs =
35+
JsonSerializer.Deserialize(Of WeatherForecastWithPOCOs)(jsonString)
36+
37+
Console.WriteLine($"Date: {weatherForecast?.Date}")
38+
Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}")
39+
Console.WriteLine($"Summary: {weatherForecast?.Summary}")
40+
41+
If weatherForecast?.DatesAvailable IsNot Nothing Then
42+
For Each dateTimeOffset As DateTimeOffset In weatherForecast.DatesAvailable
43+
Console.WriteLine($"DateAvailable: {dateTimeOffset}")
44+
Next
45+
End If
46+
47+
If weatherForecast?.TemperatureRanges IsNot Nothing Then
48+
For Each temperatureRange As KeyValuePair(Of String, HighLowTemps) In weatherForecast.TemperatureRanges
49+
Console.WriteLine($"TemperatureRange: {temperatureRange.Key}, {temperatureRange.Value.Low} - {temperatureRange.Value.High}")
50+
Next
51+
End If
52+
53+
If weatherForecast?.SummaryWords IsNot Nothing Then
54+
For Each summaryWord As String In weatherForecast.SummaryWords
55+
Console.WriteLine($"SummaryWord: {summaryWord}")
56+
Next
57+
End If
58+
End Sub
59+
60+
End Class
61+
62+
End Namespace
63+
' output:
64+
'Date: 8/1/2019 12:00:00 AM -07:00
65+
'TemperatureCelsius: 25
66+
'Summary: Hot
67+
'DateAvailable: 8/1/2019 12:00:00 AM -07:00
68+
'DateAvailable: 8/2/2019 12:00:00 AM -07:00
69+
'TemperatureRange: Cold, -10 - 20
70+
'TemperatureRange: Hot, 20 - 60
71+
'SummaryWord: Cool
72+
'SummaryWord: Windy
73+
'SummaryWord: Humid

0 commit comments

Comments
 (0)