Serializing dictionaries with JavaScriptSerializer

后端 未结 5 712
南旧
南旧 2020-12-03 22:15

Apparently, IDictionary is serialized as an array of KeyValuePair objects (e.g., [{Key:\"foo\", Value:\"bar\"}, ...]

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 22:18

    I was able to solve with JavaScriptSerializer with Linq Select:

    var dictionary = new Dictionary();
    var jsonOutput = new JavaScriptSerializer().Serialize(dictionary.Select(x => new { Id = x.Key, DisplayText = x.Value  }));
    

提交回复
热议问题