1

Trying to serialize Dictionary<string[],int>

var requestData = new Dictionary<string, object>
{
   { "games" ,new Dictionary<string[],int> { {new string[] { "1000"}, 1 } } }
             
};

var data = JsonConvert.SerializeObject(requestData);

The output JSON looks something like {"games":{"(Array)":1}} which is not what I want. Info of string[] is gone

Akshay G
  • 2,070
  • 1
  • 15
  • 33
aderw
  • 41
  • 4
  • 2
    JSON keys have to be string. I think there's no way to store an array as JSON key – Klamsi May 22 '21 at 21:23
  • You want X. You thought “serialize Dictionary” would X. Now you discover that won't X. You need to [tell us what X is](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Dour High Arch May 22 '21 at 21:46

1 Answers1

1

Why don't use this:

var requestData = new Dictionary<string, object>
        {
         { "games",new Dictionary<int, string[]>{ {  1, new string[] { "1000", "2000"} } }
         }
       };

        var data = JsonConvert.SerializeObject(requestData);

it results

{"games":{"1":["1000","2000"]}}

Serge
  • 40,935
  • 4
  • 18
  • 45