2011-07-08 79 views
3

我有一個方法:.NET WCF的Json反序列化字典<int, int>

[WebGet] 
[OperationContract] 
public int CollabSortFolder(int FolderId, Dictionary<int, int> Items) 
{ 

    Console.Write(Items); 
    return -1; 
} 

如果我試試這個網址:

http://server/CollabSortFolder?FolderId=12&Items={"1":3,"4":5,"6":7} 

Items.Count爲0

什麼是正確的語法來獲得我的字典填充?

回答

3

您可以通過編寫自定義QueryStringConverter爲您服務自定義查詢字符串參數的反序列化。下面的代碼顯示了一個可以理解你的問題格式的代碼。

public class StackOverflow_6630425 
{ 
    [ServiceContract] 
    public class Service 
    { 
     [WebGet] 
     public string CollabSortFolder(int FolderId, Dictionary<int, int> Items) 
     { 
      StringBuilder sb = new StringBuilder(); 
      sb.AppendLine("FolderId=" + FolderId); 
      foreach (var key in Items.Keys) 
      { 
       sb.AppendLine(string.Format(" Items[{0}] = {1}", key, Items[key])); 
      } 
      return sb.ToString(); 
     } 
    } 
    public class MyQueryStringConverter : QueryStringConverter 
    { 
     public override bool CanConvert(Type type) 
     { 
      return type == typeof(Dictionary<int, int>) || base.CanConvert(type); 
     } 

     public override object ConvertStringToValue(string parameter, Type parameterType) 
     { 
      if (parameterType == typeof(Dictionary<int, int>)) 
      { 
       parameter = parameter.Trim().Substring(1, parameter.Length - 2); // trimming the begin and end '{'/'}' 
       string[] pairs = parameter.Split(','); 
       Dictionary<int, int> result = new Dictionary<int, int>(); 
       foreach (string pair in pairs) 
       { 
        string[] parts = pair.Split(':'); 
        string key = parts[0].Trim(); 
        string value = parts[1].Trim(); 
        if (key.StartsWith("\"")) key = key.Substring(1); 
        if (key.EndsWith("\"")) key = key.Substring(0, key.Length - 1); 
        result.Add(int.Parse(key), int.Parse(value)); 
       } 

       return result; 
      } 

      return base.ConvertStringToValue(parameter, parameterType); 
     } 
    } 
    class MyWebHttpBehavior : WebHttpBehavior 
    { 
     protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription) 
     { 
      return new MyQueryStringConverter(); 
     } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new MyWebHttpBehavior()); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     WebClient c = new WebClient(); 
     Console.WriteLine(c.DownloadString(baseAddress + "/CollabSortFolder?FolderId=12&Items={\"1\":3,\"4\":5,\"6\":7}")); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
5

造型的關鍵,值對象的作品:

http://server/CollabSortFolder?FolderId=12&Items=[{"Key":1,"Value":3}, {"Key":2,"Value":4}]