How to create dictionary and add key–value pairs dynamically?

From post:

Sending a JSON array to be received as a Dictionary<string,string>

I’m trying to do this same thing as that post. The only issue is that I don’t know what the keys and the values are upfront. So I need to be able to dynamically add the key and value pairs and I don’t know how to do that.

Does anyone know how to create that object and add key value pairs dynamically?

I’ve tried:

var vars = [{key:"key", value:"value"}];
vars[0].key = "newkey";
vars[0].value = "newvalue";

But that doesn’t work.

17 Answers
17

Leave a Comment