我正在开发一个RESTful WCF删除内部“__type”的标签,而我目前遇到了麻烦漂亮,干净的JSON序列化的返回值。在ASP.NET宁静的WCF
A little while back, I got rid of "Key" and "Value" tags围绕我的每个键和值(通过序列化一个对象Dictionary<string, object>
)通过使用包装。
但是,当我这样做,“__type”标签开始出现。我设法通过用我的svc文件的ServiceHost标记中的WebServiceHostFactory替换WebScriptServiceHostFactory来消除“d”标记以及第一个“__type”标记。
所以我的结果是这样的:
{"Status":"Success","Data":{"__type":"SerializableDictionaryOfstringanyType:#MyNamespace.MyFolder","Key1":"Value1","Key2":"Value2","Key3":"Value3"}}
但我希望它看起来像这样:
{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}}
我的测试Web服务代码如下所示:
[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);
}
编辑:而SuccessfulResult功能看起来像这样(对不起忘记吧):
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;
}
如果有人对我怎么可能摆脱过去的小“__type”标签的任何想法,我很愿意听到他们的声音!非常感谢,并让我知道是否还有其他我可以发布的内容可能会有所帮助。
使用
<webHttp/>
这是一个不错的黑客,但一定是这样做的更多的“内置”的方式。 : -/ – BrainSlugs83