I have a Dictionary that i want to JSON-ify into a simple lookup-object. Examine this code:
var dictionary = new Dictionary<string, string>
{{"test", "value"}, {"test2", "value2"}};
If I Json encode (JSON.Encode) it, i will end up with something like this:
[{"Key": "test", "Value": "value"}, {"Key": "test2", "Value": "value2"}]
which is not what i want. I want to be able to, in JavaScript (language is not important), to lookup the Value using the Key as key. The object i want to create looks like this:
[{"test": "value"}, {"test2": "value2"}]
Is it just me, not seeing the easy solution or is there a reason why this is so hard to accomplish?
I tried to rework the dictionary object into a dynamic object structure but failed. I would really want to create a c#/.net stucture that would encode correctly, instead of writing my own json serializer...
UPDATE: I fail to see it's a duplicate. The question and answer is the reversed direction and using an external deserializer... Also trying to applying it, reversed, produces the same unwanted json result.
LAST UPDATE: While I still fail so see this as a duplicate, the second linked article hints me to the solution (in the question itself), which is very simple. The built in JSON.Encode uses the same engine as DataContractJsonSerializer does, and produces by default the unwanted result. Using JavascriptSerializer, which is also built into .net does serialization differently and luckily, produces the wanted result.