2012-02-21 16 views
5

Tôi có một WebMethod trông như thế này được sử dụng để cư trú một jqGridLàm thế nào để làm cho WebMethods serialize ExpandoObject

[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; 
    } 

} 

"Kết quả" là một System.Dynamic.ExpandoObject với các thuộc tính Items, TotalPages, TotalRecords

các json tôi nhận được trở lại trên từ webservice trông như thế này

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

Lý tưởng nhất là tôi muốn nó trở lại mà không cần tất cả các công việc kinh doanh chính và giá trị vì nó bloats ra json không cần thiết và không chơi độc đáo với jqGrid.

Có cách nào thay đổi cách ASP.NET xử lý tuần tự hóa ExpandoObject không?

+0

Một người bạn đã đề xuất phương pháp này http://stackoverflow.com/questions/5156664/how-to-flatten-an-expandoobject-returned-via-jsonresult-in-asp -net-mvc nhưng tôi không biết cách đăng ký JavaScriptConverter với JavaScriptSerializer mà chúng tôi bmethod sử dụng. –

+0

Chết tiệt, câu trả lời tôi tìm kiếm ở đây http://msdn.microsoft.com/en-us/library/bb763183.aspx về một nửa chiều xuống –

+0

Điều cần biết là bạn đã học được điều gì đó mới :) – Jull

Trả lời

4

Nghe có vẻ như bạn đã figured này ra, nhưng đây là something I threw together a while back để làm điều đó:

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; 
    } 
} 

Sau đó, bạn chỉ cần thêm nó vào phần <converters> trong web.config của bạn, như thể hiện trong bài viết MSDN mà bạn đã liên kết đến:

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

Vâng, hy vọng điều này sẽ giúp ai đó khác. Mặc dù nên xấu hổ trên MSDN. Cảm ơn bạn đã trả lời nhanh chóng của bạn Dave, tình yêu công việc, yêu thích vek tubpub –

+0

@SeanTomlins: Ack, có những gì tôi nhận được một cách mù quáng tin tưởng MSDN! Cảm ơn bạn đã chỉ ra điều đó. Tôi đã chỉnh sửa câu trả lời của mình và ping một người nào đó tại MS để hy vọng nhận được lỗi đó. –

+0

Hoạt động tốt trừ khi Giá trị là rỗng. Thêm một kiểm tra cho nó và hoạt động tốt. mục 'foreach (KeyValuePair trong expando) { if (item.Value == null) result.Add (item.Key, string.Empty); else if (item.Value.GetType() == typeof (DateTime)) result.Add (item.Key, ((DateTime) item.Value) .ToShortDateString()); else result.Add (item.Key, item.Value.ToString()); } ' – sarme

Các vấn đề liên quan