0

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.

Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
  • It looks like JSON.net does the excat reverse of my question. Perhaps it can also do the reverse-reverse :P I could go have a look, but would really want the built in JSON.Encode to output my desired result – Per Hornshøj-Schierbeck Mar 10 '14 at 11:39
  • use JsonConvert.SerializeObject – Sumeshk Mar 10 '14 at 11:41
  • What parameters/options should I call it with. I can see the default SerializeObject create the excat same unwanted json format – Per Hornshøj-Schierbeck Mar 10 '14 at 11:49
  • 1
    Although I haven't actually tried *forcing* the `DataContractSerializer` to do it right (seems futile), one of JSON.NET's listed features is actually that it doesn't support the "Nonsensical dictionary serialization" of `DataContractSerializer` - in other words, it does right what is "impossible"(?) to do with the built-in serializer (note that `JavascriptSerializer` in spite of its other issues, *can* serialize a dictionary in a sensible way). See also http://stackoverflow.com/questions/4559991/any-way-to-make-datacontractjsonserializer-serialize-dictionaries-properly – JimmiTh Mar 10 '14 at 12:09
  • The second linked Q/A actually helped me solve the problem. I would like to add the answer to the question, but i can't since it's closed. The trick was to use the JavascriptSerializer instead of JSON.Encode - both are part of the framework and thus, requires no external libraries to fix... – Per Hornshøj-Schierbeck Mar 10 '14 at 13:30

0 Answers0