0
所以,我有我喜欢使用的API有以下接口声明JSON回报C#的ServiceContract看起来不同于我期望
namespace MyAPI
{
[ServiceContract(Namespace = "http://MyAPI")]
public interface IMyAPI
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetSomething?someInt={someInt}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Dictionary<string, List<string>> GetSomething(int someInt);
}
}
在实施服务合同我像做以下
namespace MyAPI
{
[ServiceBehavior]
public class MyAPI : IMyAPI
{
public Dictionary<string, List<string>> GetSomething(int someInt)
{
Dictionary<string, List<string>> something = new Dictionary<string, List<string>>();
something["FIRST KEY"] = new List<string>();
something["SECOND KEY"] = new List<string>();
// fill up these lists...
return something;
}
}
}
然而,当我去到返回的东西我得到的东西,被格式化这样
[{"Key":"FIRST KEY","Value":[]},{"Key":"SECOND KEY","Value":[]}]
在那里我会想到JSON看看下面
{"FIRST KEY":[], "SECOND KEY":[]}
为什么两者之间的区别?我可以序列化成一个字符串,但这似乎是一个额外的(不必要的)步骤。任何帮助非常感谢
是好是坏,'System.Collections.Generic.Dictionary'序列化为System.Collections.Generic.KeyValuePair 的'阵列'。这就是“为什么”;我正在四处寻找解决办法。 –
使用Json.NET将为您提供所需的输出。 – rinukkusu
我认为这可能是主题? http://stackoverflow.com/a/10368876/424129 –