JavaScriptSerializer().Serialize(Entity Framework object)

两盒软妹~` 提交于 2020-01-15 10:22:47

问题


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:


  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.



回答2:


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



来源:https://stackoverflow.com/questions/2449980/javascriptserializer-serializeentity-framework-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!