JavaScriptSerializer()。序列化(实体框架的对象)实体、框架、对象、序列化

2023-09-08 08:48:04 作者:自由都去哪了。

可能是,它不是那么有问题的你。但我想第一次与JSON序列化。同时也阅读其他文章中stackowerflow。

我已经创建实体框架的数据模型。 然后用方法得到的所有数据对象:

 私人uqsEntities _db =新uqsEntities();
//从表sysMainTableColumns的所有数据,其中tableName值= paramtableName
公开名单< sysMainTableColumns> getDataAboutMainTable(字符串tableName值)
{
     返回(从_db.sysMainTableColumns列
                    其中,column.TableName == tableName值
                    选择列).ToList();

}
 

我的web服务:

 公共字符串getDataAboutMainTable()
{
    penta.DAC.Tables dictTable =新penta.DAC.Tables();
    变种结果= dictTable.getDataAboutMainTable(1);
    返回新JavaScriptSerializer()序列化(结果)。
}
 

和jQuery的AJAX方法

  $('#loadData)。点击(函数(){
            $阿贾克斯({
                键入:POST,
                网址:WS / ConstructorWS.asmx / getDataAboutMainTable
                数据: {},
                的contentType:应用/ JSON的;字符集= UTF-8,
                数据类型:JSON,
                成功:函数(MSG){
                    $(#jsonResponse)HTML(MSG)。

                    VAR数据=的eval((+味精+));
                    //做一些数据
                },
                错误:函数(MSG){

                }
            });
        });
 
.NET高级代码审计 第四课 JavaScriptSerializer反序列化漏洞

失败(从fairbug):

 缺少]之后元素列表[打破这个错误] VAR数据=的eval((+味精+));
 

Ajax响应(通过Firebug的,如果我删除 VAR数据=的eval((+味精+))):

{"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}}]"}

问题数据,code未能出现。我想我不会使用 JavaScriptSerializer()。序列化()方式非常好。

请告诉我,我在C#code做出什么大错?

解决方案 您不需要评估。 jQuery不会为你在指定数据类型:JSON 这是一个坏主意,直接为 JavaScriptSerializer 连载实体如果碰巧包含一个循环引用就会死亡。 请不要忘记 D !这是插入的WCF服务,以解决在某些浏览器的安全漏洞,当根对象是一个数组。

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?

解决方案

You don't need eval. jQuery does that for you when you specify dataType: "json" It's a bad idea to serialize entities directly as JavaScriptSerializer will die if one happens to contain a circular reference. 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.