180

I want to convert my Dictionary<int,List<int>> to JSON string. Does anyone know how to achieve this in C#?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
activebiz
  • 6,000
  • 9
  • 41
  • 64

16 Answers16

175

This answer mentions Json.NET but stops short of telling you how you can use Json.NET to serialize a dictionary:

return JsonConvert.SerializeObject( myDictionary );

As opposed to JavaScriptSerializer, myDictionary does not have to be a dictionary of type <string, string> for JsonConvert to work.

Community
  • 1
  • 1
Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108
  • I have a dictionary result = new Dictionary(); and when serialize itr JsonConvert.SerializeObject(result), it converts number from 1 to 1.0 . Any idea how to avoid it? – Mali Tbt Jan 14 '21 at 15:35
142

Serializing data structures containing only numeric or boolean values is fairly straightforward. If you don't have much to serialize, you can write a method for your specific type.

For a Dictionary<int, List<int>> as you have specified, you can use Linq:

string MyDictionaryToJson(Dictionary<int, List<int>> dict)
{
    var entries = dict.Select(d =>
        string.Format("\"{0}\": [{1}]", d.Key, string.Join(",", d.Value)));
    return "{" + string.Join(",", entries) + "}";
}

But, if you are serializing several different classes, or more complex data structures, or especially if your data contains string values, you would be better off using a reputable JSON library that already knows how to handle things like escape characters and line breaks. Json.NET is a popular option.

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • I think this works better in my case as its is simple. All I need to do now is to use JSON.parse() (in javascript) in my javascript and I have my data struct ready . thx – activebiz Apr 11 '11 at 09:38
  • 88
    This solution is naive at best. Use a real json serialization library. – jacobsimeon Nov 12 '12 at 22:07
  • 146
    @Jacob - Naive? Ouch. Personally, I can't justify including *another* assembly when all I need to do can be accomplished with one short and simple method. – gilly3 Nov 16 '12 at 01:21
  • 18
    One more + on gilly3 comment. Sometime you're coding on Windows Phone. You're adding zlip, twitter, google, audio processing stuff, image processing stuff. You're better implement simple method like it, and some basic HttpRequests for social media interaction if your usage is simple. When adding an assembly, you always have to deal with bugs, and with the impossibility of getting a lite version. By the way, inside json libs, there are naive methods like this and no magic. – Léon Pelletier Jun 22 '13 at 18:02
  • 15
    There should be a page on every open project telling you: "Hey, most of the time, you'll only want to execute this 10 lines algo. Here's the code." – Léon Pelletier Jun 22 '13 at 18:03
  • 40
    dict.add("RandomKey\"", "RandomValue\""); BOOOOOOOOOOOOOOOOM. Naive at best. Use a real json serialization library. – Sleeper Smith Dec 03 '14 at 03:00
  • What's a LINQ? There is nothing that doesn't require foreign library? – Jonny Dec 04 '14 at 08:01
  • 2
    @jonny - [LINQ](http://msdn.microsoft.com/en-us/library/bb397926.aspx) is built in to .Net. It is not a foreign library. – gilly3 Dec 04 '14 at 15:25
  • 7
    This is not a good answer for dictionaries with string keys or values. Double quotes, slashes and line breaks will all result in broken JSON. It should be updated to include a warning. – James Newton-King Jan 30 '15 at 22:21
  • 12
    @JamesNewton-King - Done. Funny, I was legitimately confused by Jacob's comment, and its accompanying upvotes. I was baffled again by Sleeper's comment, but it at least got me thinking. It never even occurred to me that readers were considering my method for serializing strings. "Of course this is a bad approach for strings", I thought, "that's *obvious*." I now understand the haters. This question probably ranks highly in search results for serializing dictionaries, but my answer isn't a great general purpose answer for serializing any `IDictionary`. Sorry to have been so blind! – gilly3 Jan 30 '15 at 23:20
  • It is useful for serializing of collection objects similar to key/value, such as ClaimsPrincipal.Claims – Michael Freidgeim Oct 09 '17 at 11:37
  • This did not work for me this code has produced json {"abc,1": [xyz], "abc,2": [lmn]...when tested this json in online json formatter gave me error I think better to use library for stable solution – Naga Oct 23 '18 at 05:44
  • Agreed, this is not an appropriate answer as it will not generate valid JSON in all cases – Max Dec 02 '18 at 14:31
  • 1
    The OP asked about integer values. Is this narrowly defined scope, this is still a valuable answer. – James Westgate Nov 19 '21 at 11:28
75

Json.NET probably serializes C# dictionaries adequately now, but when the OP originally posted this question, many MVC developers may have been using the JavaScriptSerializer class because that was the default option out of the box.

If you're working on a legacy project (MVC 1 or MVC 2), and you can't use Json.NET, I recommend that you use a List<KeyValuePair<K,V>> instead of a Dictionary<K,V>>. The legacy JavaScriptSerializer class will serialize this type just fine, but it will have problems with a dictionary.

Documentation: Serializing Collections with Json.NET

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • 3
    Perfect answer for asp.net mvc3 and mvc4 users – Gomes Feb 28 '13 at 12:22
  • JsonConvert.SerializeObject does not appear to handle serializing c# dictionaries to a type of enumerable collection when read back into Javascript. Rather, it makes an object where each pair in the C# collection is now a plain property/value in an object, that cant be easily enumerated like a collection. So, unless I have a bad version from nuget, Json.NET is still not adequate.in this regard. – StingyJack Oct 27 '15 at 13:23
  • Oh, yes. I was doing a similar thing already, but trying to find out another way, and stumbled upon this. I felt it may be useful to let others know to avoid the Json.NET rabbit hole for this item (works great otherwise). – StingyJack Oct 27 '15 at 13:29
26

Simple One-Line Answer

(using System.Web.Script.Serialization )

This code will convert any Dictionary<Key,Value> to Dictionary<string,string> and then serialize it as a JSON string:

var json = new JavaScriptSerializer().Serialize(yourDictionary.ToDictionary(item => item.Key.ToString(), item => item.Value.ToString()));

It is worthwhile to note that something like Dictionary<int, MyClass> can also be serialized in this way while preserving the complex type/object.


Explanation (breakdown)

var yourDictionary = new Dictionary<Key,Value>(); //This is just to represent your current Dictionary.

You can replace the variable yourDictionary with your actual variable.

var convertedDictionary = yourDictionary.ToDictionary(item => item.Key.ToString(), item => item.Value.ToString()); //This converts your dictionary to have the Key and Value of type string.

We do this, because both the Key and Value has to be of type string, as a requirement for serialization of a Dictionary.

var json = new JavaScriptSerializer().Serialize(convertedDictionary); //You can then serialize the Dictionary, as both the Key and Value is of type string, which is required for serialization.
T.Todua
  • 53,146
  • 19
  • 236
  • 237
EternalWulf
  • 742
  • 9
  • 21
26
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, List<int>> foo = new Dictionary<int, List<int>>();

            foo.Add(1, new List<int>( new int[] { 1, 2, 3, 4 }));
            foo.Add(2, new List<int>(new int[] { 2, 3, 4, 1 }));
            foo.Add(3, new List<int>(new int[] { 3, 4, 1, 2 }));
            foo.Add(4, new List<int>(new int[] { 4, 1, 2, 3 }));

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Dictionary<int, List<int>>));

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, foo);
                Console.WriteLine(Encoding.Default.GetString(ms.ToArray()));
            }
        }
    }
}

This will write to the console:

[{\"Key\":1,\"Value\":[1,2,3,4]},{\"Key\":2,\"Value\":[2,3,4,1]},{\"Key\":3,\"Value\":[3,4,1,2]},{\"Key\":4,\"Value\":[4,1,2,3]}]
Merritt
  • 2,333
  • 21
  • 23
15

Sorry if the syntax is the tiniest bit off, but the code I'm getting this from was originally in VB :)

using System.Web.Script.Serialization;

...

Dictionary<int,List<int>> MyObj = new Dictionary<int,List<int>>();

//Populate it here...

string myJsonString = (new JavaScriptSerializer()).Serialize(MyObj);
riwalk
  • 14,033
  • 6
  • 51
  • 68
  • 3
    It throws ArgumentException: Type 'System.Collections.Generic.Dictionary`2 is not supported for serialization/deserialization of a dictionary, keys must be strings or objects. – Pavel Chuchuva Sep 27 '11 at 00:05
  • See http://stackoverflow.com/questions/3685732/c-serialize-dictionaryulong-ulong-to-json – Pavel Chuchuva Sep 28 '11 at 04:50
15

If your context allows it (technical constraints, etc.), use the JsonConvert.SerializeObject method from Newtonsoft.Json : it will make your life easier.

Dictionary<string, string> localizedWelcomeLabels = new Dictionary<string, string>();
localizedWelcomeLabels.Add("en", "Welcome");
localizedWelcomeLabels.Add("fr", "Bienvenue");
localizedWelcomeLabels.Add("de", "Willkommen");
Console.WriteLine(JsonConvert.SerializeObject(localizedWelcomeLabels));

// Outputs : {"en":"Welcome","fr":"Bienvenue","de":"Willkommen"}
Ishikawa
  • 381
  • 1
  • 5
  • 11
11

In Asp.net Core use:

using Newtonsoft.Json

var obj = new { MyValue = 1 };
var json = JsonConvert.SerializeObject(obj);
var obj2 = JsonConvert.DeserializeObject(json);
Skorunka František
  • 5,102
  • 7
  • 44
  • 69
  • I referenced `System.Core` and then tried to reference `using Newtonsoft.Json` and no joy. I think `Newtonsoft` is a 3rd-party library. – vapcguy Mar 27 '17 at 22:02
  • 2
    @vapcguy Yes, Newtonsoft is 3rd-party but widely used and adopted also by MS in their products. https://www.nuget.org/packages/Newtonsoft.Json/ – Skorunka František Mar 29 '17 at 08:46
9

net core : System.Text.Json.JsonSerializer.Serialize(dict)

GeLiang
  • 91
  • 1
  • 1
8

You can use System.Web.Script.Serialization.JavaScriptSerializer:

Dictionary<string, object> dictss = new Dictionary<string, object>(){
   {"User", "Mr.Joshua"},
   {"Pass", "4324"},
};

string jsonString = (new JavaScriptSerializer()).Serialize((object)dictss);
T.Todua
  • 53,146
  • 19
  • 236
  • 237
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
4

Here's how to do it using only standard .Net libraries from Microsoft …

using System.IO;
using System.Runtime.Serialization.Json;

private static string DataToJson<T>(T data)
{
    MemoryStream stream = new MemoryStream();

    DataContractJsonSerializer serialiser = new DataContractJsonSerializer(
        data.GetType(),
        new DataContractJsonSerializerSettings()
        {
            UseSimpleDictionaryFormat = true
        });

    serialiser.WriteObject(stream, data);

    return Encoding.UTF8.GetString(stream.ToArray());
}
David Young
  • 171
  • 1
  • 2
  • We can combine this with `Dictionary` and have all JSON primitive types like intergers, floats, booleans, strings, even nulls and within one object. +1 – Christos Lytras Jun 03 '20 at 22:28
3

It seems a lot of different libraries and what not have seem to come and go over the previous years. However as of April 2016, this solution worked well for me. Strings easily replaced by ints.

TL/DR; Copy this if that's what you came here for:

    //outputfilename will be something like: "C:/MyFolder/MyFile.txt"
    void WriteDictionaryAsJson(Dictionary<string, List<string>> myDict, string outputfilename)
    {
        DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Dictionary<string, List<string>>));
        MemoryStream ms = new MemoryStream();
        js.WriteObject(ms, myDict); //Does the serialization.

        StreamWriter streamwriter = new StreamWriter(outputfilename);
        streamwriter.AutoFlush = true; // Without this, I've run into issues with the stream being "full"...this solves that problem.

        ms.Position = 0; //ms contains our data in json format, so let's start from the beginning
        StreamReader sr = new StreamReader(ms); //Read all of our memory
        streamwriter.WriteLine(sr.ReadToEnd()); // and write it out.

        ms.Close(); //Shutdown everything since we're done.
        streamwriter.Close();
        sr.Close();
    }

Two import points. First, be sure to add System.Runtime.Serliazation as a reference in your project inside Visual Studio's Solution Explorer. Second, add this line,

using System.Runtime.Serialization.Json;

at the top of the file with the rest of your usings, so the DataContractJsonSerializer class can be found. This blog post has more information on this method of serialization.

Data Format (Input / Output)

My data is a dictionary with 3 strings, each pointing to a list of strings. The lists of strings have lengths 3, 4, and 1. The data looks like this:

StringKeyofDictionary1 => ["abc","def","ghi"]
StringKeyofDictionary2 => ["String01","String02","String03","String04"]
Stringkey3 => ["someString"]

The output written to file will be on one line, here is the formatted output:

 [{
     "Key": "StringKeyofDictionary1",
     "Value": ["abc",
     "def",
     "ghi"]
 },
 {
     "Key": "StringKeyofDictionary2",
     "Value": ["String01",
     "String02",
     "String03",
     "String04",
 ]
 },
 {
     "Key": "Stringkey3",
     "Value": ["SomeString"]
 }]
mwjohnson
  • 661
  • 14
  • 26
2

You could use JavaScriptSerializer.

Twelve47
  • 3,924
  • 3
  • 22
  • 29
  • are dictionaries serializeable? – Numenor Apr 08 '11 at 15:53
  • I would have thought this would work - string json = serializer.Serialize((object)dict); – Twelve47 Apr 08 '11 at 15:56
  • 1
    @Numenor Yes, they are, but only if the Key AND Value are of type `string`. I have posted an answer here that includes that, if you want to have a look. – EternalWulf Jul 08 '14 at 13:56
  • @HowlinWulf to be more exact, the value doesn't have to be a string. But for the key, it cannot definitely be an int. Strings work best as key. – Gyum Fox Aug 14 '15 at 11:57
  • 1
    @Twelve47 Should include a sample usage, in case that link is ever moved. Otherwise this answer could become useless, one day. – vapcguy Apr 01 '17 at 00:17
1

This is Similar to what Meritt has posted earlier. just posting the complete code

    string sJSON;
    Dictionary<string, string> aa1 = new Dictionary<string, string>();
    aa1.Add("one", "1"); aa1.Add("two", "2"); aa1.Add("three", "3");
    Console.Write("JSON form of Person object: ");

    sJSON = WriteFromObject(aa1);
    Console.WriteLine(sJSON);

    Dictionary<string, string> aaret = new Dictionary<string, string>();
    aaret = ReadToObject<Dictionary<string, string>>(sJSON);

    public static string WriteFromObject(object obj)
    {            
        byte[] json;
            //Create a stream to serialize the object to.  
        using (MemoryStream ms = new MemoryStream())
        {                
            // Serializer the object to the stream.  
            DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
            ser.WriteObject(ms, obj);
            json = ms.ToArray();
            ms.Close();
        }
        return Encoding.UTF8.GetString(json, 0, json.Length);

    }

    // Deserialize a JSON stream to object.  
    public static T ReadToObject<T>(string json) where T : class, new()
    {
        T deserializedObject = new T();
        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedObject.GetType());
            deserializedObject = ser.ReadObject(ms) as T;
            ms.Close();
        }
        return deserializedObject;
    }
Karshan
  • 51
  • 3
1

Just for reference, among all the older solutions: UWP has its own built-in JSON library, Windows.Data.Json.

JsonObject is a map that you can use directly to store your data:

var options = new JsonObject();
options["foo"] = JsonValue.CreateStringValue("bar");
string json = options.ToString();
Gábor
  • 9,466
  • 3
  • 65
  • 79
0

improved mwjohnson's version:

string WriteDictionaryAsJson_v2(Dictionary<string, List<string>> myDict)
{
    string str_json = "";
    DataContractJsonSerializerSettings setting = 
        new DataContractJsonSerializerSettings()
        {
            UseSimpleDictionaryFormat = true
        };

    DataContractJsonSerializer js = 
        new DataContractJsonSerializer(typeof(Dictionary<string, List<string>>), setting);

    using (MemoryStream ms = new MemoryStream())
    {                
        // Serializer the object to the stream.  
        js.WriteObject(ms, myDict);
        str_json = Encoding.Default.GetString(ms.ToArray());

    }
    return str_json;
}
sailfish009
  • 2,561
  • 1
  • 24
  • 31