questions/43618859/how-to-create-array-of-key-value-pair-in-c

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;


// https://stackoverflow.com/questions/43618859/how-to-create-array-of-key-value-pair-in-c

	class TestClass
	{
		public static void Test()
		{
			var opts = new Dictionary<string, string>();
			opts.Add("one", "Un");
			opts.Add("two", "Deux");
			opts.Add("three", "Trois");
			
			var list = opts.Select(p => new Dictionary<string, string>() { {p.Key, p.Value }});
			
			var json = JsonConvert.SerializeObject(list);			
			Console.WriteLine(json);
		}
	}
					
	public class Program
	{
		public static void Main()
		{
            Console.WriteLine("Environment version: " + Environment.Version);
            Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
            Console.WriteLine("");
	

			TestClass.Test();			
		}
	}
;