Skip to content

Instantly share code, notes, and snippets.

@gkhays
Created January 8, 2020 15:57
Show Gist options
  • Save gkhays/e895d14161f893f16d86d9817483052b to your computer and use it in GitHub Desktop.
Save gkhays/e895d14161f893f16d86d9817483052b to your computer and use it in GitHub Desktop.

JSON Parsing in C#

In the past I have used Visual Studio for C# and .NET developement. This little project is used to create a simple .NET console program using Visual Studio Code. The goal is to confirm that for multiple value entries, the .NET parser will use the last entered value.

Getting Started

Use these instructions to get the project up and running.

Prerequisites

I used the .NET Core SDK (3.1 SDK to ensure multi-platform support).

Also install the C# extension for Visual Studio Code.

In a terminal window type the following.

dotnet new console

This will create Program.cs.

Run the program.

dotnet run

The results.

$ dotnet run
Running JSON parser order test...
JSON: { "one": 15, "one": 1.5 }
Variable set to: 1.5

References

  1. Get started with C# and Visual Studio Code
  2. How can I parse JSON with C#?
using System;
using System.Text.Json;
namespace org.gkh.json
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Running JSON parser order test...");
var json = "{ \"one\": 15, \"one\": 1.5 }";
Console.WriteLine("JSON: " + json);
var simplejson = JsonSerializer.Deserialize<SimpleJson>(json);
Console.WriteLine("Variable set to: " + simplejson.one);
}
}
public class SimpleJson
{
public decimal one { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment