title: RESTful ASP.NET webservice - How can I return JSON-serialized Dictionary<string,object>?
body: <p>I have a RESTful web service that returns JSON-serialized data.  It can successfully serialize a <code>Dictionary&lt;string, string&gt;</code> object, but I would like each function to be able to return <code>Dictionary&lt;string, object&gt;</code>.</p>
  
  <p>When I return <code>Dictionary&lt;string, string&gt;</code>, I get the expected JSON response.  But when I try to return <code>Dictionary&lt;string, object&gt;</code>, I get this response:</p>
  
  <blockquote>
    <p>ReadResponse() failed: The server did not return a response for this
    request.</p>
  </blockquote>
  
  <p>So, ON TO THE CODE!  <strong>Here is the <code>Dictionary&lt;string, object&gt;</code> code that fails</strong>:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
  public Dictionary&lt;string, object&gt; Test(String Token, String Id)
  {
      Dictionary&lt;string, object&gt; testresults = new Dictionary&lt;string, object&gt;();
      testresults.Add("Test1Key", "Test1Value");
  
      Dictionary&lt;string, string&gt; innertestresults = new Dictionary&lt;string, string&gt;();
      innertestresults.Add("InnerTest1Key", "InnerTest1Value");
      innertestresults.Add("InnerTest2Key", "InnerTest2Value");
  
      testresults.Add("Test2Key", innertestresults);
  
      return testresults;
  }
  </code></pre>
  
  <p>And, just for kicks/reference, <strong>here is the <code>Dictionary&lt;string,string&gt;</code> code that works perfectly</strong>:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
  public Dictionary&lt;string, string&gt; Test(String Token, String Id)
  {
      Dictionary&lt;string, string&gt; testresults = new Dictionary&lt;string, string&gt;();
      testresults.Add("Test1Key", "Test1Value");
      testresults.Add("Test2Key", "Test2Value");
      testresults.Add("Test3Key", "Test3Value");
  
      return testresults;
  }
  </code></pre>
  
  <p>If anybody has any ideas of how to get this to work (or any alternative ways of doing this to get the same end result), please do let me know!  I'm pretty open on how to do this.</p>
  
  <p><strong>On the topic of usage</strong>... the reason I need the mix is so that I can return results like this (where the "Data" part could be ANYTHING... not necessarily something with the keys ID, Type, and MaxUsers):</p>
  
  <pre><code>{"Status":"Success","Data":{"ID":"1234","Type":"Live","MaxUsers":"5"}}
  {"Status":"Failure","Error":"ID does not exist"}
  </code></pre>
  
  <p>Thank you all very much!</p>