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

后端 未结 5 1663
别跟我提以往
别跟我提以往 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 07:00

    avoiding the "__type" in json...

    in the webapi.config, there are several options (look to the last one):

            // To disable tracing in your application, please comment out or remove the following line of code
            // For more information, refer to: http://www.asp.net/web-api
            //config.EnableSystemDiagnosticsTracing();
    
            // Use camel case for JSON data.
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    
            // The setting will let json.net to save type name in the payload if the runtime type is different with the declare type. 
            // When you post it back, json.net will deserialize the payload to the type you specified in the payload.
            // source: http://stackoverflow.com/questions/12858748/asp-net-webapi-posting-collection-of-subclasses
            //config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
    

提交回复
热议问题