Make ASP.NET WCF convert dictionary to JSON, omitting “Key” & “Value” tags

后端 未结 5 1648
别跟我提以往
别跟我提以往 2020-11-27 05:58

Here\'s my dilemma. I\'m using a RESTful ASP.NET service, trying to get a function to return a JSON string in this format:

{\"Test1Key\":\"Test1Value\",\"Te         


        
5条回答
  •  庸人自扰
    2020-11-27 06:44

    Expanding slightly on @MarkisT's excellent solution, you can modify the serialization constructor to recreate one of these dictionaries from the same JSON (thus allowing you to take an AjaxDictionary as a service parameter), as follows:

    public AjaxDictionary( SerializationInfo info, StreamingContext context )
    {
         _Dictionary = new Dictionary();
    
         foreach (SerializationEntry kvp in info)
         {
             _Dictionary.Add((TKey)Convert.ChangeType(kvp.Name, typeof(TKey)), (TValue)Convert.ChangeType(kvp.Value, typeof(TValue)));
         }
    }
    

提交回复
热议问题