加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何使WebMethods序列化ExpandoObject

发布时间:2020-12-16 03:27:05 所属栏目:asp.Net 来源:网络整理
导读:我有一个看起来像这样的WebMethod用于填充jqGrid [System.Web.Script.Services.ScriptService]public class MyWebService: System.Web.Services.WebService{ [WebMethod] [Authorize(Roles = "Admin")] public object GetPeople(bool _search,double nd,int
我有一个看起来像这样的WebMethod用于填充jqGrid

[System.Web.Script.Services.ScriptService]
public class MyWebService: System.Web.Services.WebService
{
    [WebMethod]
    [Authorize(Roles = "Admin")]
    public object GetPeople(bool _search,double nd,int rows,int page,string sidx,string sord)
    {
        var tbl = new DynamicModel("ConnStr",tableName: "Person",primaryKeyField: "ID");
        var results = tbl.Paged(orderBy: sidx + " " + sord,currentPage: page,pageSize: rows);
        return results;
    }

}

“results”是一个System.Dynamic.ExpandoObject,其属性为Items,TotalPages,TotalRecords

我从webservice上回来的json看起来像这样

{
"d": [{
    "Key": "TotalRecords","Value": 1
},{
    "Key": "TotalPages",{
    "Key": "Items","Value": [
        [{
            "Key": "Row","Value": 1
        },{
            "Key": "ID",{
            "Key": "Name","Value": "Test Template"
        }]
    ]
}]
}
} // Don't know why firebug put this extra bracket

理想情况下,我更喜欢它没有所有Key和Value业务,因为它不必要地膨胀了json并且与jqGrid不能很好地协作.

有没有办法改变ASP.NET处理ExpandoObject序列化的方式?

解决方法

听起来你已经想到了这一点,但这里有 something I threw together a while back这样做:

public class ExpandoObjectConverter : JavaScriptConverter {
  public override IEnumerable<Type> SupportedTypes {
    get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(ExpandoObject) })); }
  }

  public override IDictionary<string,object> Serialize(object obj,JavaScriptSerializer serializer) {
    ExpandoObject expando = (ExpandoObject)obj;

    if (expando != null) {
      // Create the representation.
      Dictionary<string,object> result = new Dictionary<string,object>();

      foreach (KeyValuePair<string,object> item in expando) {
        if (item.Value.GetType() == typeof(DateTime))
          result.Add(item.Key,((DateTime)item.Value).ToShortDateString());
        else
          result.Add(item.Key,item.Value.ToString());
      }

      return result;
    }
    return new Dictionary<string,object>();
  }

  public override object Deserialize(IDictionary<string,object> dictionary,Type type,JavaScriptSerializer serializer) {
    return null;
  }
}

然后,您只需将其添加到< converters> web.config中的部分,如您链接到的MSDN文章中所示:

<configuration>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization>
          <converters>
            <add name="ExpandoObjectConverter" type="ExpandoObjectConverter"/>
          </converters>
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读