From 4021ef50f1532f77b6ff090feab1a9c6173f1511 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Wed, 23 Sep 2026 14:03:59 +0200 Subject: [PATCH] Get value by key from JObject: retarget net10.0, make Program.cs run the four approaches - Both projects move from net7.0 to net10.0. - Newtonsoft.Json 13.0.2 to 13.0.4; Microsoft.NET.Test.Sdk 17.3.2 to 18.10.1; xunit 2.4.2 to 2.9.3; xunit.runner.visualstudio 2.4.5 to 4.0.0; coverlet.collector 3.1.2 to 10.0.1. - Program.cs constructs JObjectManipulation once and calls the four methods in the article's order, so running the sample prints the output the article shows. - GetValuesUsingValueMethod reads year with Value, matching the int cast the other approaches use. - TestData.cs uses a plain raw string literal; the JSON has no interpolation. --- .../GetValueByKeyFromJObject.csproj | 4 ++-- .../GetValueByKeyFromJObject/JObjectManipulation.cs | 2 +- .../GetValueByKeyFromJObject/Program.cs | 9 +++++++-- .../GetValueByKeyFromJObject/TestData.cs | 2 +- .../GetValueByKeyFromJObject/Tests/Tests.csproj | 10 +++++----- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/GetValueByKeyFromJObject.csproj b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/GetValueByKeyFromJObject.csproj index 684f7060f3..22de9cb403 100644 --- a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/GetValueByKeyFromJObject.csproj +++ b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/GetValueByKeyFromJObject.csproj @@ -1,13 +1,13 @@ Exe - net7.0 + net10.0 enable disable - + \ No newline at end of file diff --git a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/JObjectManipulation.cs b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/JObjectManipulation.cs index f032bbb80f..08f30aaa32 100644 --- a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/JObjectManipulation.cs +++ b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/JObjectManipulation.cs @@ -42,7 +42,7 @@ public int GetValuesUsingValueMethod() var name = jsonObject.Value("name"); var make = jsonObject.Value("make"); var model = jsonObject.Value("model"); - var year = jsonObject.Value("year"); + var year = jsonObject.Value("year"); var amount = jsonObject.Value("price") .Value("amount"); diff --git a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/Program.cs b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/Program.cs index f256f3048c..4488acd7fb 100644 --- a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/Program.cs +++ b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/Program.cs @@ -1,3 +1,8 @@ -// See https://aka.ms/new-console-template for more information +using GetValueByKeyFromJObject; -Console.WriteLine("Application Started"); +var jObjectManipulation = new JObjectManipulation(); + +jObjectManipulation.GetValuesUsingIndex(); +jObjectManipulation.GetValuesUsingValueMethod(); +jObjectManipulation.GetValuesUsingSelectToken(); +jObjectManipulation.GetValuesUsingTryGetValue(); diff --git a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/TestData.cs b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/TestData.cs index 014eeede82..914ecfaf9e 100644 --- a/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/TestData.cs +++ b/json-csharp/GetValueByKeyFromJObject/GetValueByKeyFromJObject/TestData.cs @@ -4,7 +4,7 @@ public class TestData { public string GenerateSingleJsonObject() { - const string car = $$""" + const string car = """ { "name": "Charger", "make": "Dodge", diff --git a/json-csharp/GetValueByKeyFromJObject/Tests/Tests.csproj b/json-csharp/GetValueByKeyFromJObject/Tests/Tests.csproj index c8a3949457..ead6f71d91 100644 --- a/json-csharp/GetValueByKeyFromJObject/Tests/Tests.csproj +++ b/json-csharp/GetValueByKeyFromJObject/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all