开发者

JavaScriptSerializer().Serialize(Entity Framework object)

开发者 https://www.devze.com 2022-12-23 04:00 出处:网络
May be, it is not so problematic for you. but i\'m trying first time with json serialization. and also read other articles in stackowerflow.

May be, it is not so problematic for you. but i'm trying first time with json serialization. and also read other articles in stackowerflow.

I have created Entity Framework data model. then by method get all data from object:

private uqsEntities _db = new uqsEntities();
//get all data from table sysMainTableColumns where tableName=paramtableName
public List<sysMainTableColumns> getDataAboutMainTable(string tableName)
{
     return (from column in _db.sysMainTableColumns
                    where column.TableName==tableName
                    select column).ToList();

}

my webservice:

public string getDataAboutMainTable()
{
    penta.DAC.Tables dictTable = new penta.DAC.Tables();
    var result = dictTable.getDataAboutMainTable("1");
    return new JavaScriptSerializer().Serialize(result);
}

and jQuery ajax method

$('#loadData').click(function() {
            $.ajax({
                type: "POST",
                url: "WS/ConstructorWS.asmx/getDataAboutMainTable",
                data: "{}"开发者_如何转开发,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    $("#jsonResponse").html(msg);

                    var data = eval("(" + msg + ")");
                    //do something with data
                },
                error: function(msg) {

                }
            });
        });

Fails (from fairbug):

missing ] after element list [Break on this error] var data = eval("(" + msg + ")");

ajax Response (by Firebug if I remove var data = eval("(" + msg + ")")):

{"d":"[{\"ID\":1,\"TableName\":\"1\",\"Name\":\"d\",\"FullName\":\"f\",\"Type\":\"nvarchar(50)\",\"MeasurementUnit\":\"t         \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":1}],\"IsTemporary\":false}},{\"ID\":2,\"TableName\":\"1\",\"Name\":\"e\",\"FullName\":\"e\",\"Type\":\"int\",\"MeasurementUnit\":\"r         \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":2}],\"IsTemporary\":false}}]"}

problem with data, code fails there. and i think i'm not use JavaScriptSerializer().Serialize() method very well.

Please, tell me, what a big mistake I made in C# code?


  1. You don't need eval. jQuery does that for you when you specify dataType: "json"
  2. It's a bad idea to serialize entities directly as JavaScriptSerializer will die if one happens to contain a circular reference.
  3. Don't forget the d! That's inserted by WCF services to work around a security hole in some browsers when the root object is an array.


Have you tried debugging with Firebug or Fiddler to see what the JSON content looks like?

0

精彩评论

暂无评论...
验证码 换一张
取 消