Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:28:38+00:00 2026-05-26T03:28:38+00:00

I’m developing a RESTful WCF, and am currently having trouble getting nice, clean JSON-serialized

  • 0

I’m developing a RESTful WCF, and am currently having trouble getting nice, clean JSON-serialized return values.

A little while back, I got rid of “Key” and “Value” tags around each of my keys and values (caused by serializing a Dictionary<string, object> object) by using a wrapper.

But when I did that, “__type” tags started showing up. I managed to get rid of the “d” tag, along with the first “__type” tag by replacing WebScriptServiceHostFactory with WebServiceHostFactory in the ServiceHost tag of my svc file.

So my result looks like this:

{"Status":"Success","Data":{"__type":"SerializableDictionaryOfstringanyType:#MyNamespace.MyFolder","Key1":"Value1","Key2":"Value2","Key3":"Value3"}}

But I want it to look like this:

{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}}

My test webservice code looks like this:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public SerializableDictionary<string, object> Test1(String Token, String Id)
{
    DataTable testTable = new DataTable();
    testTable.Columns.Add("Key1", typeof(System.String));
    testTable.Columns.Add("Key2", typeof(System.String));
    testTable.Columns.Add("Key3", typeof(System.String));

    DataRow testRow = testTable.NewRow();
    testRow["Key1"] = "Value1";
    testRow["Key2"] = "Value2";
    testRow["Key3"] = "Value3";

    testTable.Rows.Add(testRow);

    return SuccessfulResult(testTable);
}

EDIT: And the SuccessfulResult function looks like this (sorry for forgetting it):

private SerializableDictionary<string, object> SuccessfulResult(DataTable dt = null)
{
    SerializableDictionary<string, object> result = new SerializableDictionary<string, object>();
    result.Add("Status", "Success");

    if (dt == null || dt.Rows.Count != 1)
        return result;

    SerializableDictionary<string, object> dct = new SerializableDictionary<string, object>();
    foreach (DataColumn currCol in dt.Rows[0].Table.Columns)
        dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString());

    result.Add("Data", dct);

    return result;
}

If anybody has any ideas on how I might get rid of that last little “__type” tag, I would love to hear them! Thanks very much, and let me know if there’s anything else I can post that might be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:28:39+00:00Added an answer on May 26, 2026 at 3:28 am

    Okay, fixed it up – but ended up doing things a little differently. WCF seems to insist on putting in those internal “__type” tags when it serializes Dictionaries, but for some reason, it doesn’t do the same thing with Streams.

    Here’s the new webservice method code (just the return type has changed):

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public Stream Test1(String Token, String StreamId)
    {
        DataTable testTable = new DataTable();
        testTable.Columns.Add("Key1", typeof(System.String));
        testTable.Columns.Add("Key2", typeof(System.String));
        testTable.Columns.Add("Key3", typeof(System.String));
    
        DataRow testRow = testTable.NewRow();
        testRow["Key1"] = "Value1";
        testRow["Key2"] = "Value2";
        testRow["Key3"] = "Value3";
    
        testTable.Rows.Add(testRow);
    
        return SuccessfulResult(testTable);
    }
    

    And here’s the new SuccessfulResult function (which is what made the difference):

    private Stream SuccessfulResult(DataTable dt = null)
    {
        Dictionary<string, object> returnDict = new Dictionary<string, object>();
        returnDict.Add("Status", "Success");
    
        Dictionary<string,object> dct = new Dictionary<string,object>();
        foreach (DataColumn currCol in dt.Rows[0].Table.Columns)
            dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString());
    
        returnDict.Add("Data", dct);
    
        string sResponse = json.Serialize(returnDict);
        byte[] byResponse = Encoding.UTF8.GetBytes(sResponse);
    
        return new MemoryStream(byResponse);
    }
    

    Now the output looks exactly how I want it to look:

    {"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}}
    

    Anyway, I hope this example helps somebody else 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.