C#

C# Dicationary to Json

Korokke 2021. 8. 20. 00:51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
Dictionary<stringint> example = new Dictionary<stringint>();
example.Add("abc"100);
 
string MyDictionaryToJson(Dictionary<stringint> dict)
    {
        var entries = dict.Select(d =>
            string.Format("\"{0}\": {1}", d.Key, string.Join(",", d.Value)));
        return "{" + string.Join(",", entries) + "}";
    }
 
print(MyDictionaryToJson(example));
cs