Skip to content

Instantly share code, notes, and snippets.

@TheWaWaR
Last active December 19, 2017 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheWaWaR/06d4e41e6b5ca90464566db6e80f2b32 to your computer and use it in GitHub Desktop.
Save TheWaWaR/06d4e41e6b5ca90464566db6e80f2b32 to your computer and use it in GitHub Desktop.
C# Json.Net serialize Dictionary<string, object>. You can run it on: http://rextester.com/JOT81793
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
Dictionary<string, object> points = new Dictionary<string, object>
{
{ "James", 9001 },
{ "Jo", "This is a string" },
{ "Jess", null }
};
points.Add("array", new object[]{"a", "b", 3});
string json = JsonConvert.SerializeObject(points, Formatting.Indented);
Console.WriteLine(json);
// {
// "James": 9001,
// "Jo": 3474,
// "Jess": 11926
// "array": [
// "a",
// "b",
// 3
// ]
// }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment